1

I'm working on some regex with JavaScript. I have now created a regex that checks if a string has two or more of the same letter following each other. I would want to create a regex that checks if a word / string contains two or more of one particular letter, no matter if they are after each other or just in the same word / string.

It would need to match: drama and anaconda, but not match: lame, kiwi or tree.

This is the regex in JS.

const str = "anaconda"; 
str.match(/[a]{2,}/);
2
  • 1
    Maybe all you need is /a.*a/ (or /a[^a]*a/) to test if a string contains at least two as? Commented Feb 11, 2021 at 20:22
  • Something like \S*(\S)\S*\1\S* regex101.com/r/VNnfjP/1 Commented Feb 11, 2021 at 20:29

2 Answers 2

2

Use

\w*(\w)\w*\1\w*

See proof

EXPLANATION

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))
Sign up to request clarification or add additional context in comments.

4 Comments

Yes for word characters I think this would be it.
Thanks for the answer, with great explanation! But in this case the regex would match for example the word: tree and kiwi, and I only want the match to happen if the word in question has two or more: As in the given string. Any input on this issue?
Could this be a solution: [a]*([a])\w*\1[a]* ?
@Krullmizter Not quite.
0

My thought process was something like this:

  1. A word can start with any Alphabet
  2. A word can end with any Alphabet
  3. Similar letters can have zero or multiple alphabets between them
  4. If any of it's letters are similar and it fits the criteria above then accept the word
     regex = /[a-z]*([a-z])[a-z]*\1+[a-z]*/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.