我正在使用JavaScript开发一些正则表达式。现在,我创建了一个正则表达式,用于检查字符串是否具有两个或更多个相同的字母,且每个字母后面都紧跟着。我想创建一个正则表达式,以检查一个单词/字符串是否包含一个特定字母中的两个或多个,而不管它们是紧挨着还是在同一个单词/字符串中。
它需要匹配:戏剧和水蟒,但不匹配:la子,猕猴桃或树。
这是JS中的正则表达式。
const str = "anaconda";
str.match(/[a]{2,}/);
使用
\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))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句