我需要一个 javascript 中的正则表达式,它替换<!-- foo -->
字符串中的所有出现。
我试过了:
thestring.replace(/<!--[\s\S]*-->/gm, "")
但这不起作用,甚至破坏了我的文字。
我不能使用,.*
因为它不包括换行符。
示例多行注释:
<!-- foo
bar
baz-->
尝试:
thestring.replace(/<!--[^]*-->/gm, "")
或使用 lazy?
使前一个量词找到最短匹配项:
thestring.replace(/<!--[^]*?-->/gm, "")
^
<!-- a comment here --> oops, ignore? -->
^^^^^^^^^^^^^^^^^^^^^^^
作为旁注,如果您尝试使用 regexp 解析 html,我建议您查看此处的最佳答案:RegEx match open tags except XHTML self-contained tags
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句