Romain Derie
Romain Derie

Reputation: 516

regex to match a username with no consecutive spaces

I am struggling to make a javascript regex to satisfy the following:

  1. The first character has to be alphabetical ([a-zA-Z])
  2. The rest can be any letters, any numbers, hyphen, dot, underscore and spaces
  3. BUT no consecutive spaces, e.g: two or more spaces in a row
  4. The length has to be between 3 and 25 (inclusive)

So here is what I found Regex:

/^[a-z][\s\w.-]{3,24}$/i

My current regex works, but won't be able to test whether the user has written consecutive spaces. How can I test for that?

Upvotes: 2

Views: 3474

Answers (1)

anubhava
anubhava

Reputation: 785196

This should work for you:

/^[a-z](?!.* {2})[ \w.-]{2,24}$/gmi

RegEx Demo

Upvotes: 5

Related Questions