您好我试图在HTML文档中删除所有javascript评论(//
)。例如
<html>
<img src="http://example.com/img.jpg" />
<script>
//Some comments
gallery: {
enabled: true,
navigateByImgClick: true,
preload: [0,1] // Will preload 0 - before current, and 1 after the current image
},
</script>
</html>
以下是我的正则表达式代码[^(http?s:)|ftp]\/\/(.*)
。这很有效。但我想确定,有没有办法改进这段代码。 ?
答案 0 :(得分:1)
你的正则表达式说&#34;匹配fhpts?:()
中没有的任何字符,后跟两个字面斜杠和任何与该行相关的字符&#34;
Normlly你想做(?<!http)(?<!https)(?<!ftp)\/\/.*
,但JavaScript并不支持lookbehinds(对每个人来说都很令人失望)所以考虑这样做:
.replace(/\s\/\/.*/,"")
这需要评论在他们之前有空格(他们几乎总是这样做) - 它并不完美,但它是我现在能想到的最好的XD