11

I have a simple string e.g.

var s = "<p>Hello World!</p><p>By Mars</p>";

How do I convert s to a jQuery object? My objective is to remove the <p>s and </p>s. I could have done this using regex, but that's rather not recommended.

2
  • 2
    You just should have read the documentation a bit better: api.jquery.com/jQuery/#jQuery2 Commented Jan 1, 2012 at 9:09
  • @hippietrail: The latter. The accepted answer i.e. .text() does fulfill what I needed. Commented Oct 19, 2012 at 14:04

3 Answers 3

10

In the simplest form (if I am understanding correctly):

var s = "<p>Hello World!</p><p>By Mars</p>";
var o = $(s);
var text = o.text();

Or you could use a conditional selector with a search context:

// load string as object, wrapped in an outer container to use for search context
var o = $("<div><p>Hello World!</p><p>By Mars</p></div>");

// sets the context to only look within o; otherwise, this will return all P tags
var tags = $("P", o); 

tags.each(function(){
    var tag = $(this); // get a jQuery object for the tag
    // do something with the contents of the tag
});

If you are parsing large amounts of HTML (for example, interpreting the results of a screen scrape), use a server-side HTML parsing library, not jQuery (tons of posts on here about HTML parsing).

Sign up to request clarification or add additional context in comments.

2 Comments

$("P", o); would not work though. It's the same as o.find('p'), meaning it looks for p-descendants of the selected elements. But the selected elements are already the p elements.
@FelixKling - you are absolutely right...I shouldn't write code at 2am. I will update my example, because I think a contextual selector is a useful option to have.
1

To get all the strings there use

var s = "<p>Hello World!</p><p>By Mars</p>";
var result = "";
$.each($(s), function(i){
    result += " " + $(this).html();
});

Comments

0

if you don't want regex, why don't u just:

var s = "<p>Hello World!</p><p>By Mars</p>";
s = s.replace('<p>', '').replace('</p>', '');

8 Comments

Unless that is exactly the expression the OP needs, a string replace will break if the tags are cased differently or there are attributes within the tag.
though it changes only the first occurence =\ I would not say that s = s.replace(/<\/?p>/i, ''); is bad
oh sure, if it comes to attribute parsing it's a simple regex also... s = s.replace(/<\/?p[^>]*>/i, '');
General consensus on SO (me included) is that using regular expressions to parse HTML is not the ideal solution: stackoverflow.com/questions/1732348/…. It works in simple cases, but it is very brittle even compared to something like jQuery which uses the browser's DOM or a robust server-side parser that does a character by character parse and accounts for all the possibilities which exist in valid/invalid HTML.
This answer has exactly the same problem as using regex. Though I agree that if the "simple string" mentioned in the question is going to reliable follow that format then a string replace (with or without regex) would do the trick fine.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.