match "//" comments with regex but not inside a quote
Posted
by
Wireless102
on Stack Overflow
See other posts from Stack Overflow
or by Wireless102
Published on 2010-12-31T05:57:48Z
Indexed on
2010/12/31
21:54 UTC
Read the original article
Hit count: 264
I need to match and replace some comments. for example:
$test = "the url is http://www.google.com";// comment "<-- that quote needs to be matched
I want to match the comments outside of the quotes, and replace any "
's in the comments with "
's.
I have tried a number of patterns and different ways of running them but with no luck.
The regex will be run with javascript to match php "//" comments
UPDATE: I took the regex from borkweb below and modified it. used a function from http://ejohn.org/blog/search-and-dont-replace/ and came up with this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function t_replace(data){
var q = {}, ret = "";
data.replace(/(?:((["'\/]*(("[^"]*")|('[^']*'))?[\s]*)?[\/\/|#][^"|^']*))/g, function(value){
q[key] = value;
});
for ( var key in q ){
ret = q[key];
}
var text = data.split(ret);
var out = ret + text[1];
out = out.replace(/"/g,""");
out = out.replace(/'/g,"'");
return text[0] + out;
}
</script>
</head>
<body>
<script type="text/javascript">
document.write(t_replace("$test = \"the url is http://www.google.com\";// c'o\"mment \"\"\"<-- that quote needs to be matched")+"<br>");
document.write(t_replace("$test = 'the url is http://www.google.com';# c'o\"mment \"\"\"<-- that quote needs to be matched"));
</script>
</body>
</html>
it handles all the line comments outside of single or double quotes. Is there anyway I could optimize this function?
© Stack Overflow or respective owner