[英]Word not adjacent to word in regex?
我有一些不好的话,如:
'aaa','bbb','ccc'
(坏话也可以,“约翰”,“保罗”,“ringo”)(对不起@cyilan)
我不想让bad word
立即跟随另一个/同一个bad word
aaa
可以跟随非坏词, then
要遵循一个坏词:
...aaaRoyibbb... //ok
...cccRoyiaaa... //ok
...aaabbb...// NOT OK
...cccbbb...// NOT OK
...cccccc...// NOT OK
一个坏词不允许立即跟随另一个/同一个坏词
我尝试了一些正则表达但没有成功..
任何帮助都感激不尽
var str = "...aaabbb...";
if(!str.test(/(?:aaa|bbb|ccc){2}/)){
// passed
}
聊天透露OP真正想要的是:
/^(?!(?:aaa|bbb|ccc)|.*(?:aaa|bbb|ccc){2}|.*(?:aaa|bbb|ccc)$)/
但是真的:
^(?!(?:aaa|bbb|ccc)\b|.*\b(?:aaa|bbb|ccc)\s+(?:aaa|bbb|ccc)\b|.*\b(?:aaa|bbb|ccc)$)
match = subject.match(/\b([a-z]{3})(?:(?!\1)|(?=\1))[a-z]+\b/i);
if (match != null) {
// matched text: match[0]
// match start: match.index
// capturing group n: match[n]
} else {
// Match attempt failed
}
您正在寻找的解决方案是\\ b。 \\ b被定义为分词。 如果它跟随空格或数字,如果以下文本是字母,则匹配。 如果它跟随字母,如果以下不是字母(即不是连续的单词),则匹配。 它可以有效地用作锚标记,如下所示:
\byourword\b
它会匹配:
This is yourword, but not mine.
yourword is found in this sentence.
但它不匹配:
When yourwordis found in other words, this will not match.
And ifyourword is at the end of another word, it will still not match.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.