Junior Dev
Junior Dev

Reputation: 11

Regular expression Cannot have more than 3 letters together

Regular expression Cannot have more than 3 letters together (i.e. "Joe4u" is OK, but "JoeL4u" is not) I have been trying several approaches but non of them seem to work, I have tried Lookahead and Lookbehind but nothing This is just one of the several tries I have done.

^(?=[A-Za-z]*[A-Za-z]{0,2}[^A-Za-z]*)(?=.{8,})

Upvotes: 0

Views: 299

Answers (2)

Junior Dev
Junior Dev

Reputation: 11

This LIF_HKN 's answer, thanks to him for the help

^([A-Za-z]{0,3}[^A-Za-z]+)*[A-Za-z]{0,3}$

You can play with it here Debuggex Demo

Upvotes: 1

akuhn
akuhn

Reputation: 27793

Just use ! to reverse the test

if (!/[a-zA-Z]{4}/.test(str)) {
  ...
}

How this works step by step

  • /[a-zA-Z]{4}/.test(str) checks if the string contains four consecutive letters
  • !... reverses the result

Upvotes: 1

Related Questions