Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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,在给定的随机字符串中:”#id1.class1.1class[price=“23.23”].7ytr“ 我想替换模式-点后跟数字,后跟{n}字母数字,如:.8acb8 但当它被“[]”包围时,请忽略它,如-[price=“23.23”] 目前,我只编写了需要查找的模式,而不忽略[]: str.replace(/\.(\d+\w+)/g, '[class="$1"]'); 您将使用负面外观标题: \.\d\w+(?![^[]*\]) 解释: \.\d\w+ # Any dot

在给定的随机字符串中:
”#id1.class1.1class[price=“23.23”].7ytr“

我想替换模式-点后跟数字,后跟{n}字母数字,如:
.8acb8

但当它被“[]”包围时,请忽略它,如-
[price=“23.23”]

目前,我只编写了需要查找的模式,而不忽略[]:

str.replace(/\.(\d+\w+)/g, '[class="$1"]');

您将使用负面外观标题:

\.\d\w+(?![^[]*\])
解释

\.\d\w+         # Any dot followed by a number followed by alpha-numeric characters
(?![^[]*\])     # Which is not inside brackets (Negative Lookahead)

试试这个。抓取捕获或匹配。参见演示


\.\d\w+(?=[^\[\]]*(?:\[\\]$)
我想避开[]内的其他变量,您不能跳过正则表达式them@user959210如果您使用
match
,则它将跳过
[]
。这是此处使用的内容。
\[[^]]*\]|(\.\d\w+)