我正在编写一个脚本,需要拆分包含 html 标签和文本的字符串。我正在尝试隔离标签并删除文本。
例如,我想要这个:
string = "<b>Text <span>Some more text</span> more text</b>";
拆分成这样:
separation = string.split(/some RegExp/);
并成为:
separation[0] = "<b>";
separation[1] = "<span>";
separation[2] = "</span>";
separation[3] = "</b>";
如果有任何帮助或建议,我将不胜感激。
最佳答案
您可能需要查看 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>"]
关于Javascript RegEx - 使用标签之间的文本拆分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10464997/