我想匹配字符串中除 #*# 模式之外的所有字符。我下面的正则表达式适用于下面的 match1 等情况,但它也会匹配不在 ## 模式中的 # 或 ,如下面的 match2 所示,并因此失败。如果下面的正则表达式一起出现,如何修复它们以匹配 #*#?
var string1 = 'Hello#*#World#*#Some other string#*#false'
var string2 = 'Hello#*#World#*#Some #other #string#*#false'
// This would match
var match1 = string1.match(/^([^#*#]+)#\*#([^#*#]+)#\*#([^#*#]+)#\*#([^#*#]+)$/);
// This would no longer match since it matches other #'s that are not in a #*# pattern
var match2 = string2.match(/^([^#*#]+)#\*#([^#*#]+)#\*#([^#*#]+)#\*#([^#*#]+)$/);
此外,匹配应该匹配模式之间的整个单词。所以对于 match1 来说就是
[ 'Hello#*#World#*#Some other string#*#false',
'Hello',
'World',
'Some other string',
'false',
index: 0,
input: 'Hello#*#World#*#Some other string#*#false',
groups: undefined ]
最佳答案
你可以试试这个。
var string1 = 'Hello#*#World#*#Some other string#*#false'
var string2 = 'Hello#*#World#*#Some #other #string#*#false'
// This would match
var match1 = string1.match(/[^(#\*#)]+/g);
var match2 = string2.match(/[^(#\*#)]+/g);
console.log(match1);
console.log(match2);
关于javascript - 正则表达式匹配除字符组合之外的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53638756/