Javascript RegEx - Split Html-string -


i'm working on script , need split strings contain both html tags , text. i'm trying isolate text , elimanate tags

for example, want this:

string = '<p><span style="color:#ff3366;">a</span></p><p><span style="color:#ff3366;text-decoration:underline;">b</span></p><p><span style="color:#ff3366;text-decoration:underline;"><em>c</em></span></p>'; 

to split this:

separation = string.split(/some regexp/); 

and become:

separation[0] = "<span style="color:#ff3366;">a</span>"; separation[1] = "<span style="color:#ff3366;text-decoration:underline;">b</span>"; separation[2] = "<span style="color:#ff3366;text-decoration:underline;"><em>c</em></span>"; 

after split sepeartion string this:

stringnew = '<span style="color:#ff3366;">a</span>';  extendedseperation = stringnew.split(/some regexp/);  extendedseperation[0] = "a"; extendedseperation[1] = "style="color:#ff3366;"; 

don't use regex reasons explained in comments.

instead, this:

create invisible node:

node = $("<div>").css("display", "none"); 

attach body:

$("body").append(node); 

now inject html node:

node.html(myhtmlstring); 

now can traverse dom tree , extract/render like, this:

ptags = node.find("p") // return <p> tags 

to content of tag use:

ptags[0].html() 

finally, clear node do:

node.html(""); 

this should enough going.

this way leverage internal parser of browser, suggested in comments.


Comments