匹配"//"注释与正则表达式但不在引号内

Wir*_*102 2 javascript php regex

我需要匹配并替换一些评论.例如:

$test = "the url is http://www.google.com";// comment "<-- that quote needs to be matched
Run Code Online (Sandbox Code Playgroud)

我希望匹配引号之外的注释,并用"注释替换注释中的任何注释&quot;.

我已经尝试了许多模式和不同的运行方式,但没有运气.

正则表达式将使用javascript运行以匹配php"//"注释

更新:我从下面的borkweb拿了正则表达式并修改它.使用了http://ejohn.org/blog/search-and-dont-replace/中的一个函数,得出了这个:

<!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>
Run Code Online (Sandbox Code Playgroud)

它处理单引号或双引号之外的所有行注释.无论如何我可以优化这个功能吗?

更新2:它不处理此字符串

document.write(t_replace("$test //= \"the url is http://www.google.com\"; //c'o\"mment \"\"\"<-- that quote needs to be matched")+"<br>");
Run Code Online (Sandbox Code Playgroud)

Tha*_*hai 12

您可以使用正则表达式同时匹配所有字符串和注释.如果它是一个字符串,您可以将其替换为自身,不更改,然后处理特殊情况以进行注释.

我想出了这个正则表达式:

"(\\[\s\S]|[^"])*"|'(\\[\s\S]|[^'])*'|(\/\/.*|\/\*[\s\S]*?\*\/)
Run Code Online (Sandbox Code Playgroud)

共有3个部分:

  • "(\\[\s\S]|[^"])*" 用于匹配双引号字符串.
  • '(\\[\s\S]|[^'])*' 用于匹配单引号字符串.
  • (\/\/.*|\/\*[\s\S]*?\*\/) 用于匹配单行和多行注释.

replace函数检查匹配的字符串是否为注释.如果不是,请不要更换.如果是,请更换"'.

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

测试运行:

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
Run Code Online (Sandbox Code Playgroud)