我不擅长正则表达式,试图制作 2 个正则表达式。
正则表达式1:
All specified words in any order but nothing else. (repetition allowed).
正则表达式2:
All specified words in any order but nothing else. (repetition not allowed).
单词:
aaa, bbb, ccc
字符串:
aaa ccc bbb
aaa ccc
aaa bbb ddd ccc
bbb aaa bbb ccc
Regex1 将以上字符串计算为:
true -> all word present in any order
false -> bbb is missing
false -> unknown word 'ddd'
false -> repetition not allowed
Regex2 将以上字符串计算为:
true -> all word present in any order
false -> bbb is missing
false -> unknown word 'ddd'
true -> all word present in any order and repetition is allowed
我的尝试
/^(?=.*\baaa\b)(?=.*\bbbb\b)(?=.*\bccc\b).*$/
询问学习目的,所以请详细说明。
最佳答案
无重复 regex101
^(?:(aaa|bbb|ccc)(?!.*?\b\1) ?\b){3}$
并重复 regex101
^(?=.*?\baaa)(?=.*?\bbbb)(?=.*?\bccc)(?:(aaa|bbb|ccc) ?\b)+$
还有两个想法。右侧 regex101 处的正则表达式解释。
关于javascript - 正则表达式:任何顺序的特定单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55116510/