Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

php - match "//" comments with regex but not inside a quote

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 &quot;'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,"&quot;");
                out = out.replace(/'/g,"&apos;");
                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?

UPDATE 2: it does not handle this string

document.write(t_replace("$test //= "the url is http://www.google.com"; //c'o"mment """<-- that quote needs to be matched")+"<br>");
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can have a regexp to match all strings and comments at the same time. If it's a string, you can replace it with itself, unchanged, and then handle a special case for comments.

I came up with this regex:

"(\[sS]|[^"])*"|'(\[sS]|[^'])*'|(//.*|/*[sS]*?*/)

There are 3 parts:

  • "(\[sS]|[^"])*" for matching double quoted strings.
  • '(\[sS]|[^'])*' for matching single quoted strings.
  • (//.*|/*[sS]*?*/) for matching both single line and multiline comments.

The replace function check if the matched string is a comment. If it's not, don't replace. If it is, replace " and '.

function t_replace(data){
    var re = /"(\[sS]|[^"])*"|'(\[sS]|[^'])*'|(//.*|/*[sS]*?*/)/g;
    return data.replace(re, function(all, strDouble, strSingle, comment) {
        if (comment) {
            return all.replace(/"/g, '&quot;').replace(/'/g, '&apos;');
        }
        return all;
    });
}

Test run:

Input: $test = "the url is http://www.google.com";// c'o"mment """<-- that quote needs to be matched
Output: $test = "the url is http://www.google.com";// c&apos;o&quot;mment &quot;&quot;&quot;<-- that quote needs to be matched

Input: $test = 'the url is http://www.google.com';# c'o"mment """<-- that quote needs to be matched
Output: $test = 'the url is http://www.google.com';# c'o"mment """<-- that quote needs to be matched

Input: $test //= "the url is http://www.google.com"; //c'o"mment """<-- that quote needs to be matched
Output: $test //= &quot;the url is http://www.google.com&quot;; //c&apos;o&quot;mment &quot;&quot;&quot;<-- that quote needs to be matched

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...