Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex for matching a word with multiples of each letter

Sorry I have been playing around with regex for a while now and not getting anywhere.

Say my word is ‘NEIGH’:

nnnneeeiggghhhhh 
Neeeiighh
Neigh
Neeeighh
Nnnneeeigh

Any of the above should be able to match ^.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My closest example:

const regex = /n+e+i+g+h?(.)\1/gm;

if (regex.exec(message.content) !== null) {
  // Do something
}

>Solution :

You can use

/n+e+i+g+h+/i
/\bn+e+i+g+h+\b/i

where + quantifier is used after each letter, and the i flag enables case insensitive matching.

If the whole word must be matched, add \b word boundaries.

See the regex demo.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading