-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