我正在尝试使用正则表达式匹配,例如,'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 :(得分:1)
类似的东西:
^[^"]*?(0+)[^"]*?$
应该有用。
这基本上匹配包含0且不包含引号的行。零在组内捕获。
let str = `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.`
str.split("\n").forEach(s => console.log((/^[^"]*?(0+)[^"]*?$/g).exec(s)));