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