简体   繁体   English

Javascript RegEx-在标签之间使用文本进行拆分

[英]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. 我正在处理脚本,需要拆分同时包含html标签和文本的字符串。 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: 您可能需要改用String.match

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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