0
我知道这个问题的变种已被问了很多次,但我发现其他解决方案都没有为我工作。PHP的正则表达式来删除单行注释
我只是试图想出一个正则表达式来删除可能出现在一些JavaScript中的任何单行注释。
理想情况下,它有一些方法可以检查两个正斜杠不是任何实际脚本的一部分。
$("a[href='#top']").click(function() { // This should be removed
$('html, body').animate({
scrollTop:0
}, 'slow');
return false;
});
var SomeURL = 'http://www.google.com'; // This comment should be removed but the URL should not
我有这样的:
...但是,打破我的代码,因为它剔除了网址的使用单线沿着评论
这将是一个与正则表达式相关的痛苦,因为您正在寻找一个上下文相关的模式。 E.G'var a ='// some string'' should be be removed,but'var a ='some string'// here here' should be be removed。 – bassxzero
在[这里](https://stackoverflow.com/questions/5419154/how-to-remove-single-line-comments-in-php)中提出的解决方案似乎工作! – tjadli
@bassxzero我想如果正则表达式只是检查'//'后面是否存在分号来忽略它。我只是不知道该怎么写。 – JROB