Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将两个正则表达式合并为一个进行替换_Javascript_Regex - Fatal编程技术网

Javascript 将两个正则表达式合并为一个进行替换

Javascript 将两个正则表达式合并为一个进行替换,javascript,regex,Javascript,Regex,我有以下字符串 var query = `'4'="$USER$" AND '5'=""$USER$"" AND '6'="""$USER$""" AND '7'=""""$USER$"""""`; 两个几乎相似的正则表达式,一个用单引号替换匹配项,另一个用三引号替换匹配项: var a = /(^|[^"])"(\$USER\$|\$TIME\$)"(?!")/g var b = /(^|[^"])"""(\$USER\$|\$TIME\$)"""(?!")/g 我可以这样说: var f

我有以下字符串

var query = `'4'="$USER$" AND '5'=""$USER$"" AND '6'="""$USER$""" AND '7'=""""$USER$"""""`;
两个几乎相似的正则表达式,一个用单引号替换匹配项,另一个用三引号替换匹配项:

var a = /(^|[^"])"(\$USER\$|\$TIME\$)"(?!")/g
var b = /(^|[^"])"""(\$USER\$|\$TIME\$)"""(?!")/g
我可以这样说:

var firstQueryResult = query.replace(a, '$1$2');
var finalResult = firstQueryResult.replace(b, '$1"$2"') // replaces with additional one pair of quotes
但我想知道这是否可以在一个regexp中完成:

=\s*(?=("(")"|")\$)"+(\$USER\$|\$TIME\$)\1(?=[^"]|$)

说明

 =\s*           # Match `=` and any number of spaces
 (?=            # Start of a positive lookahead (pl:1)
     (              # Start of capturing group (1)
        "(")"       # Match 3 double quotes in a row (and capture one of them as 2nd CP)
     |              # OR
        "           # One single `"`
     )              # End of capturing group (1)
     \$             # That is followed by a `$`
 )              # End of positive lookahead (pl:1)
 "+             # Match any number of `"`
 (                  # Start of capturing group (2)
    \$USER\$        # Match `$USER$`
    |               # OR
    \$TIME\$        # Match `$TIME$`
 )              # End of capturing group (2)
 \1             # Followed by same number of quotes captured by 1st CP
 (?=            # Start of a positive lookahead (pl:2)
    [^"]            # Followed by a non-`"` character
    |               # OR
    $               # End of string
 )              # End of positive lookahead (pl:2)
JavaScript:

string.replace(/=\s*(?=("(")"|")\$)"+(\$USER\$|\$TIME\$)\1(?=[^"]|$)/g, '=\2\3\2');
这也避免了不平衡的引号。

RegEx:

=\s*(?=("(")"|")\$)"+(\$USER\$|\$TIME\$)\1(?=[^"]|$)

说明

 =\s*           # Match `=` and any number of spaces
 (?=            # Start of a positive lookahead (pl:1)
     (              # Start of capturing group (1)
        "(")"       # Match 3 double quotes in a row (and capture one of them as 2nd CP)
     |              # OR
        "           # One single `"`
     )              # End of capturing group (1)
     \$             # That is followed by a `$`
 )              # End of positive lookahead (pl:1)
 "+             # Match any number of `"`
 (                  # Start of capturing group (2)
    \$USER\$        # Match `$USER$`
    |               # OR
    \$TIME\$        # Match `$TIME$`
 )              # End of capturing group (2)
 \1             # Followed by same number of quotes captured by 1st CP
 (?=            # Start of a positive lookahead (pl:2)
    [^"]            # Followed by a non-`"` character
    |               # OR
    $               # End of string
 )              # End of positive lookahead (pl:2)
JavaScript:

string.replace(/=\s*(?=("(")"|")\$)"+(\$USER\$|\$TIME\$)\1(?=[^"]|$)/g, '=\2\3\2');

这也避免了不平衡的引号。

我试图理解
([^ | ^“])
但我不能。你认为那部分应该做什么?@NiettheDarkAbsol,建议这样做。这是一个解决后面缺少外观问题的方法。它能起作用吗?@revo,第一个正则表达式似乎能起作用,但第二个不起作用……让我检查一下
查询的值是什么?
?我正在试图理解
([^ |^^^“]))
但我不能。“你认为那个角色应该做什么?”有人建议说。这是一个解决缺少lookbehind的方法。它能工作吗?@revo,第一个正则表达式似乎工作正常,但第二个不工作。。。让我检查一下
query
的值是什么?谢谢,我从regexp中删除了
=
。在替换字符串中使用
'\2\3\2'
是否与
'$2$3$2'
相同?您最好将其包括在内,否则会出现两个问题:1-引擎需要更多步骤才能匹配它们。2-它将匹配开场白
“”$USER中的不平衡引号$“
@Maximusok,谢谢。只是想让我的regexp更通用。这也很有趣,为什么它需要更多的步骤来匹配?那么这个呢:在替换字符串中使用“\2\3\2”是否与“$2$3$2”相同?您能将此信息添加到您的答案中吗?谢谢,我从regexp中删除了
=
。在替换字符串中使用
'\2\3\2'
是否与
'$2$3$2'
相同?您最好将其包括在内,否则会出现两个问题:1-引擎需要更多步骤才能匹配它们。2-它将匹配开头部分的不平衡引号
“$USER$”
@Maximusok,谢谢。只是想让我知道regexp更通用。还有一点很有趣,为什么它需要更多的步骤来匹配?这是怎么回事:在替换字符串中使用“\2\3\2”是否与“$2$3$2”相同?请将此信息添加到您的答案中?