开发者

Javascript Regex Pattern

开发者 https://www.devze.com 2023-03-23 02:39 出处:网络
I\'m trying to create a regex pattern that allows the user to create a username with the following specific开发者_JAVA技巧ations. (For the purposes of this initial pattern, I\'m only using standard am

I'm trying to create a regex pattern that allows the user to create a username with the following specific开发者_JAVA技巧ations. (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:

  • Spidy
  • Spidy.Man
  • Ama-za-zing_Spidy

Examples of invalid inputs:

  • Extreme___Spidy (repeated underscores)
  • The_-_Spidy (repeated special characters)
  • _ _ SPIDY _ _ (starts and ends with special characters)


Sounds like this pattern:

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


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

精彩评论

暂无评论...
验证码 换一张
取 消