我正在用JavaScript做一些调整。我现在已经创建了一个正则表达式,它检查一个字符串是否有两个或多个相同的字母相互跟随。我想要创建一个regex来检查一个单词/字符串是否包含一个特定字母中的两个或多个字母,无论它们是在彼此之间,还是在同一个单词/字符串中。
它需要匹配:lame和anaconda,但不匹配:lame、kiwi或tree。
这是JS中的正则表达式。
const str = "anaconda";
str.match(/[a]{2,}/);发布于 2021-02-11 22:56:23
使用
\w*(\w)\w*\1\w*请参阅证明
解释
NODE EXPLANATION
--------------------------------------------------------------------------------
\w* word characters (a-z, A-Z, 0-9, _) (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
\w word characters (a-z, A-Z, 0-9, _)
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
\w* word characters (a-z, A-Z, 0-9, _) (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\1 what was matched by capture \1
--------------------------------------------------------------------------------
\w* word characters (a-z, A-Z, 0-9, _) (0 or
more times (matching the most amount
possible))发布于 2021-02-11 22:33:27
我的思维过程是这样的:
regex = /[a-z]*([a-z])[a-z]*\1+[a-z]*/https://stackoverflow.com/questions/66162157
复制相似问题