views:

129

answers:

4

how to split <p><span>Hello</span></p> to <span>Hello</span> using javascript

var text = "<p><span>Hello</span></p>";

remember:I don't know what contain <p>, I don't know if <p> has any attribute or not

I found the answer !

var patt=/^<p.*?>(.*)<\/p>$/i;
var result=patt.exec(text);
alert(result[1]); 

thank's ring0 & w3schools http://www.w3schools.com/jsref/jsref_obj_regexp.asp

but there is problem ! it doesn't work with

aa<p><span>Hello</span></p>aa
+11  A: 

Don't do string manipulations on this, make use of the DOM.

// create a dummy container div element
var tempDiv = document.createElement('div');
// insert the desired html inside this container
tempDiv.innerHTML = "<p><span>Hello</span></p>";
// find the first para, and get its html
tempDiv.getElementsByTagName("p")[0].innerHTML; // contains "<span>Hello</span>"

Try this snippet.

If you are using a framework like jQuery, you can use:

$("<p><span>Hello</span></p>").html()

Try this snippet.

Anurag
Why go through the trouble of inserting it into the DOM? Simple `split` `split`.
Josh K
@Josh k - The solution doesn't insert into the DOM; it _creates_ a DOM element, but doesn't insert it.
LeguRi
and you downvoted me because you think `split` is cool? really? really? ok now let's also add a class - `<p class="wtf"><span>Hello</span></p>`. go split now.
Anurag
Clearly the best solution is to use a regular expression ;)
Michael Mrozek
@Anurag: I downvoted you because it didn't answer his question (as phrased), how do I split `[phase]`. He made no mention of using classes, so why should I care? Considering the updated answer, this solution is good. Upvoted.
Josh K
@Josh - The OP did say `split` in his question, but reading more about it, he was not literally referring to the function, but rather chopping the paragraph tags out of that string, which can be achieved in numerous ways, and none of them would be clearly wrong.
Anurag
It's a `split`... no, it's a `regex`... no, it's `jQuery`!
Peter Ajtai
A: 

What do you really want?

  • You can pull this out with a substring: text.substr(3, 18)
  • You can use a regex: text.match(/<span>([^<]+)<\/span>/)
  • You can stick this is the DOM and parse it out.

You'll need to explain the question better.

Paul Schreiber
A: 

Note I used p> and </p instead of the full tag. I'm not sure if the split will give you an array with an empty first index if you use the full tag. I'm too lazy to test it given the rather poor question phrasing.

var text = "<p><span>Hello</span></p>";
var foo = text.split("p>");
var bar = foo[1].split("</p");
alert(bar[0]);
Josh K
Thanks, but if <p> has any attribute, what can i do.can you split using <p not p>
faressoft
@faressoft: This won't work if `<p>` has an attribute.
Josh K
+1  A: 

A regular expression that takes care of removing p attributes

var new = text.replace(/^<p[^>]*>(.*)<\/p>$/i, "$1");

Or a version with .*?

var new = text.replace(/^<p.*?>(.*)<\/p>$/i, "$1");


And if <pre> or <param> may start the text, you have to prevent a match

var new = text.replace(/^<p\b.*?>(.*)<\/p>$/i, "$1");


edit to answer your second question

To remove whatever is before / after

var new = text.replace(/^.*<p\b[^>]*>(.*)<\/p>.*$/i, "$1");

But if you want to remove all <p...> and all </p>, you should use the two lines

var new = text.replace(/<p\b.*?>/ig, "");
new = text.replace(/<\/p>/ig, "");
ring0
See my question again.I will accept your answer if you complete it.
faressoft
Please see the **edit** part
ring0
And added the `\b` that was missing in the last answer :-)
ring0