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
Javascript JS Regex:如果字符串不在引号中,则匹配该字符串,引号可能是闭合的,也可能不是闭合的_Javascript_Regex - Fatal编程技术网

Javascript JS Regex:如果字符串不在引号中,则匹配该字符串,引号可能是闭合的,也可能不是闭合的

Javascript JS Regex:如果字符串不在引号中,则匹配该字符串,引号可能是闭合的,也可能不是闭合的,javascript,regex,Javascript,Regex,我正在尝试使用正则表达式来匹配,例如,“0”字符。 但是,我只想匹配不在引号中的0,它可能不会被关闭 下面是一个输入字符串的示例,其中指出了我要匹配的0 abc"0 "hi 0 bye "00 0 //match here If 0 //match here If (00) //match here, with both zeros being in the same capture group. hell0 w0rld //match here, each zero being differe

我正在尝试使用正则表达式来匹配,例如,“0”字符。 但是,我只想匹配不在引号中的0,它可能不会被关闭

下面是一个输入字符串的示例,其中指出了我要匹配的0

abc"0
"hi 0 bye
"00
0 //match here
If 0 //match here
If (00) //match here, with both zeros being in the same capture group.
hell0 w0rld //match here, each zero being different
"0"
"00"
5*0 // match here
00 //match here, with both zeros being in the same capture group.
我尝试过在其他线程上修改代码片段,但没有效果。 首先,我认为我可以更改
(?!\B“[^”]*)0(?![^”]*“\B)
(发布在另一个线程上)以满足我的需要,但我无法做到

谢谢。

类似于:

^[^"]*?(0+)[^"]*?$
应该有用

这基本上是匹配包含0且不包含引号的行。零在一个组中被捕获

let str=`abc“0
“你好,再见
"00
0//在此处匹配
如果0//在此处匹配
If(00)//此处匹配,两个零位于同一捕获组中。
hell0 w0rld//此处匹配,每个零都不同
"0"
"00"
5*0//在此处匹配
00//此处匹配,两个零位于同一捕获组中`

str.split(“\n”).forEach(s=>console.log((/^[^”]*?(0+[^”]*?$/g.exec));
谢谢!这也很明显。请确保在零上添加贪婪运算符,以便它获得所有可用的零,对吗?很好的观点!已更新以处理包含1个以上零的示例。