Spidy
Spidy

Reputation: 39992

Javascript Regex Pattern

I'm trying to create a regex pattern that allows the user to create a username with the following specifications. (For the purposes of this initial pattern, I'm only using standard american English alphabet.

The first character must be an alphabetic letter (uppercase or lowercase). [a-zA-Z] The last character must be a alphanumeric (uppercase or lowercase). [a-zA-Z0-9] Any characters in between must be letters or numbers with one rule:

The user can use a period(.), dash(-), or underscore(_) but it must be followed by an alphanumeric character. So no repeats of one or more of these characters at a time.

I've tried the following regex pattern but am not getting the results I was hoping for. Thanks for taking the time to help me on this.

^[a-zA-Z]([a-zA-Z0-9]+[._-]?[a-zA-Z0-9]+)+$

EDIT

It might actually be working the way I expected. But I'm always getting two matches returned to me. The first one being the entire valid string, the second being a shortened version of the first string usually chopping off the first couple of characters.

Examples of valid inputs:

Examples of invalid inputs:

Upvotes: 1

Views: 574

Answers (2)

Petar Ivanov
Petar Ivanov

Reputation: 93020

^[a-zA-Z]([._-]?[a-zA-Z0-9]+)*$

Upvotes: 1

Kerrek SB
Kerrek SB

Reputation: 476990

Sounds like this pattern:

^[a-zA-Z]([._-]?[a-zA-Z0-9])*$

Upvotes: 5

Related Questions