user433351
user433351

Reputation: 225

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.

Upvotes: 1

Views: 3698

Answers (1)

Ry-
Ry-

Reputation: 224905

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>"]

Upvotes: 7

Related Questions