-1
如前所述,我试图用RegExp将一些html标签转换为其他自定义标签。如何使用RegExp转换嵌套<span>标签
我的解决方案是不是在嵌套的标签工作如下:
解决方案1:
var str = '<span style=\"font-size: x-large;\"><span style=\"color: red;\">HELLO WORLD</span></span>';
var txt = str.replace(/<span style=\"(font-size|color): (.*?);\">(.*?)<\/span>/gim,"[$2]$3[/$2]");
的例外结果:
[x-large][red]HELLO WORLD[/red][/x-large]
实际结果:
[x-large]<span style="color: red;">[/x-large]</span>
解决方案2:
var str = '<span style=\"font-size: x-large;\"><span style=\"color: red;\">HELLO WORLD</span></span>';
var txt = str.replace(/<span style=\"(font-size|color): (.*?);\">(.*?)<\/span>/gim,"[$2]$3[/$2]");
txt = txt.replace(/<span style=\"(font-size|color): (.*?);\">(.*?)<\/span>/gim,"[$2]$3[/$2]");
的例外结果:
[x-large][red]HELLO WORLD[/red][/x-large]
实际结果:
[x-large][red]HELLO WORLD[/x-large][/red]
Regexp不够智能,无法处理涉及嵌套的HTML等语言。 – 2017-08-10 03:19:33
[你不应该](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454)。 –
[RegEx匹配除XHTML自包含标记之外的开放标记]的可能重复(https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – pchaigno