Home > Back-end >  Regex for matching a word with multiples of each letter
Regex for matching a word with multiples of each letter

Time:12-18

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 ^.

My closest example:

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

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

CodePudding user response:

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.

  • Related