评论与正则表达式但不在报价内

评论与正则表达式但不在报价内

本文介绍了匹配“//”评论与正则表达式但不在报价内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我想匹配以外的评论报价,并用& quot; 替换评论中的任何

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.

正则表达式将运行javascript以匹配php//评论

The regex will be run with javascript to match php "//" comments

更新:
我从下面的borkweb获取了正则表达式并对其进行了修改。使用并提出这个:

<!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?

更新2:
它不处理这个字符串

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>");


推荐答案

您可以使用正则表达式匹配所有字符串和注释同时。如果它是一个字符串,你可以用它自己替换它,保持不变,然后处理一个特殊的评论案例。

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:

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

共有3个部分:


  • (\\ [\\\\ S] | [^])*用于匹配双引号字符串。

  • '(\\ [\\\\ S] | [^'])*'用于匹配单引号字符串。

  • (\ / \ /.* | \ / \ * [\\\\ S] *?\ * \ /)用于匹配单行和多行注释。

  • "(\\[\s\S]|[^"])*" for matching double quoted strings.
  • '(\\[\s\S]|[^'])*' for matching single quoted strings.
  • (\/\/.*|\/\*[\s\S]*?\*\/) 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 = /"(\\[\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;
    });
}

试运行:

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

这篇关于匹配“//”评论与正则表达式但不在报价内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 21:54