[英]Combine 2 JavaScript RegEx groups with AND and quantifier
Unfortunately I try in vain to write a suitable JavaScript RegEx, which matches the following case: 不幸的是,我徒劳地编写了一个适合以下情况的合适的JavaScript RegEx:
The RegEx for the 2 point is probably: 2点的RegEx可能是:
^(\\s*\\S+(?:\\s+\\S+){1,4}\\s*)$
demo ^(\\s*\\S+(?:\\s+\\S+){1,4}\\s*)$
演示
The RegEx for the 3 point is probably: 3点的正则表达式可能是:
^(?=.*(There\\b|\\This\\b|\\bDog)).*$
demo ^(?=.*(There\\b|\\This\\b|\\bDog)).*$
演示
Now I have the problem to combine this 2 regex into one. 现在,我有问题将这2个正则表达式合并为一个。 I've tried something like: ^(\\s*\\S+(?:\\s+\\S+){1,5}\\s*)(?=.*(There\\b|\\bThis\\b|Dog)).*$
demo 我已经尝试过类似的操作: ^(\\s*\\S+(?:\\s+\\S+){1,5}\\s*)(?=.*(There\\b|\\bThis\\b|Dog)).*$
演示
How I can combine there 2 RegEx correctly? 我怎样才能正确地结合在那里2 RegEx?
Some Examples: 一些例子:
Hello I am under 6 words
-> negative, no trigger word Hello I am under 6 words
->否定,无触发字
Hello I am under dog words
-> positive under 6 words + trigger word (dog) Hello I am under dog words
->积极在6词以下+触发词(狗)
Hello I am dog but with a longer string
-> negative because more then 6 words Hello I am dog but with a longer string
->负数,因为超过6个字
Dog
-> positive, under 6 words + trigger word(dog) Dog
->正数,少于6个字+触发词(狗)
You may put the lookahead from the second regex after ^
in the first regex and use word boundaries around the alternation: 您可以将第一个正则表达式中的第二个正则表达式中的^
放在第一个正则表达式中,并在替换处使用单词边界:
^(?=.*\b(?:There|This|Dog)\b)\s*\S+(?:\s+\S+){1,5}\s*$
See the regex demo . 参见regex演示 。 Note the case insensitive modifier in the demo ( /^(?=.*\\b(?:There|This|Dog)\\b)\\s*\\S+(?:\\s+\\S+){1,5}\\s*$/i
). 注意演示中不区分大小写的修饰符( /^(?=.*\\b(?:There|This|Dog)\\b)\\s*\\S+(?:\\s+\\S+){1,5}\\s*$/i
)。
It matches strings with 6 words or less that must contain There
, This
or Dog
as whole words. 它匹配不超过6个单词的字符串,且整个单词必须包含There
, This
或Dog
。
See the regex graph : 参见正则表达式图 :
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.