need some regex help to strip out // comments but not http:// urls
| From: | Daevid Vincent | Date: | Tue, 28 May 2013 21:17:06 +0000 |
| Subject: | need some regex help to strip out // comments but not http:// urls | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-321221@lists.php.net to get a copy of this message | ||
I'm adding some minification to our cache.class.php and am running into an
edge case that is causing me grief.
I want to remove all comments of the // variety, HOWEVER I don't want to
remove URLs...
Given some example text here with carefully crafted cases:
// another comment here
<iframe src="http://foo.com">
function bookmarksite(title,url){
if (window.sidebar) // firefox
window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
var elem = document.createElement('a');
elem.setAttribute('href',url);
elem.setAttribute('title',title);
elem.setAttribute('rel','sidebar');
elem.click();
}
else if(document.all)// ie
window.external.AddFavorite(url, title);
}
I've tried so many variations and hunted on StackOverflow, Google, etc. and
what would seem like a task already solved, doesn't seem to be.
This is "close", but still matches //foo.com (omitting the : of course)
\s*(?!:)//.*?$ (count it as '/m' multiline)
This ultimately ends up in a PHP line of code like so:
$sBlob = preg_replace("@\s*//.*?$@m",'',$sBlob);
Here are some other links of stuff I've found and tried to experiment with
varying degrees.
http://stackoverflow.com/questions/4568410/match-comments-with-regex-but-not
-inside-a-quote
http://stackoverflow.com/questions/611883/regex-how-to-match-everything-exce
pt-a-particular-pattern
http://stackoverflow.com/questions/11863847/regex-to-match-urls-but-not-urls
-in-hyperlinks
http://stackoverflow.com/questions/643113/regex-to-strip-comments-and-multi-
line-comments-and-empty-lines