简体   繁体   中英

Javascript RegEx - Split using text between tags

I'm working on a script and need to split strings which contain both html tags and text. I'm trying to isolate the tags and eliminate the text.

For example, I want this:

string = "<b>Text <span>Some more text</span> more text</b>";

to be split like this:

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

and become:

separation[0] = "<b>";
separation[1] = "<span>";
separation[2] = "</span>";
separation[3] = "</b>";

I would really appreciate any help or advice.

You'll probably want to look into String.match instead:

var str = "<b>Text <span>Some more text</span> more text</b>";
var separation = str.match(/<[^]+?>/g);

console.log(separation); // ["<b>", "<span>", "</span>", "</b>"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM