简体   繁体   English

在正则表达式中与单词不相邻的单词?

[英]Word not adjacent to word in regex?

i have some bad words set like : 我有一些不好的话,如:

'aaa','bbb','ccc'

( bad words can be also , "john","paul","ringo") ( sorry @cyilan) (坏话也可以,“约翰”,“保罗”,“ringo”)(对不起@cyilan)

i dont want to allow bad word immediately followed by another/same bad word 我不想让bad word 立即跟随另一个/同一个bad word

aaa can be followed by a non-bad word and then to be followed by a bad word : aaa可以跟随非坏词, then要遵循一个坏词:

  ...aaaRoyibbb...  //ok
  ...cccRoyiaaa...  //ok

   ...aaabbb...// NOT OK
   ...cccbbb...// NOT OK
   ...cccccc...// NOT OK

a bad word is not allowed to be immediately followed with another/same bad word 一个坏词不允许立即跟随另一个/同一个坏词

I've tried some regexps but with no success.. 我尝试了一些正则表达但没有成功..

any help will be much appreciated 任何帮助都感激不尽

var str = "...aaabbb...";
if(!str.test(/(?:aaa|bbb|ccc){2}/)){
    // passed
}

Chat revealed that what OP really wanted was: 聊天透露OP真正想要的是:

/^(?!(?:aaa|bbb|ccc)|.*(?:aaa|bbb|ccc){2}|.*(?:aaa|bbb|ccc)$)/

But really really: 但是真的:

^(?!(?:aaa|bbb|ccc)\b|.*\b(?:aaa|bbb|ccc)\s+(?:aaa|bbb|ccc)\b|.*\b(?:aaa|bbb|cc‌​c)$)
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
}

The solution you're looking for is \\b. 您正在寻找的解决方案是\\ b。 \\b is defined as a word break. \\ b被定义为分词。 If it follows white space or numbers, it matches if the following text is letters. 如果它跟随空格或数字,如果以下文本是字母,则匹配。 If it follows letters, it matches if the following is not letters (ie not a continuous word). 如果它跟随字母,如果以下不是字母(即不是连续的单词),则匹配。 It can be effectively used as an anchor tag, like this: 它可以有效地用作锚标记,如下所示:

\byourword\b

It would match: 它会匹配:

This is yourword, but not mine.
yourword is found in this sentence.

But it would not match: 但它不匹配:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM