简体   繁体   中英

How to create a username regular expression with JavaScript?

Following are the rules for user name:

  1. Usernames can only use alpha-numeric characters.

  2. The only numbers in the username have to be at the end. There can be zero or more of them at the end. Username cannot start with the number.

  3. Username letters can be lowercase and uppercase.

  4. Usernames have to be at least two characters long. A two-character username can only use alphabet letters as characters.

I tried using the following:

for (1.) I did /^[a-zA-Z0-9]/

for (2.) I did /[0-9]*$/

for (3.) already did it in (1.)

for (4.) (Am not too sure about this) /../...Trying to combine all these into a single regex statement I did: /^[a-zA-Z0-9][0-9]*$../

You can use a negative lookahead to assert that the string does not start with a char a-zA-Z followed by an optional digit.

Then start the match with 1+ chars a-zA-Z and end with optional digits.

^(?![A-Za-z]\d?$)[a-zA-Z]+\d*$

Explanation

  • ^ Start of string
  • (?![A-Za-z]\\d?$) Negative lookahead, assert not a char a-zA-Z followed by an optional digit and end of string
  • [a-zA-Z]+\\d* Match 1+ chars a-zA-Z followed by optional digits so that the string can not start with a digit
  • $ End of string

Regex demo

 const regex = /^(?![A-Za-z]\\d?$)[a-zA-Z]+\\d*$/; [ "tt", "tt2", "test", "a11", "t2", "t", "1", "t1t" ].forEach(s => console.log(`${s} ==> ${regex.test(s)}`));

As great as the answer is, it might not be very easy for a new bee This does exactly the same thing

^([az]{2,}|[az][0-9]{2,})[0-9]*$

This way ^ signifies the beginning position of the string,it signifies the beginning of the string The () is used to define a group and the group defined in this case is all lowercase alphabets with a minimum lenght of 2, because the {} is used to specify the length of the string Then the pipe symbol used there | signifies "or" then the next expression is [az][0-9]{2,} which means all lowercase alphabets followed by an integer whose minimum length should be 2. Finally after that group they can have zero or more occurance of an integer this is enforced using the * symbol and $ denotes the end of the string So this regex reads thus: from the begining of the string,it should begin with lowercase alphabets with a minimum length of 2 or a lowercase alphabet and a number but the numbers length can't be less than 2 then you can have a series of integers or numbers come after them or you can have none This is a very direct and constrained solution and probably should be used for learning purposes or in cases that it's just required as so, in real life applications there may be need for more flexible regular expressions

More breakdown:

^([az]{2,}|[az][0-9]{2,})[0-9]*$

'^` asserts position at start of a line

1st Capturing Group ([az]{2,}|[az][0-9]{2,})

1st Alternative [az]{2,}

Match a single character present in the list below [az]{2,}

{2,} Quantifier — Matches between 2 and unlimited times, as many times as possible, giving back as needed (greedy)

az a single character in the range between 'a' (index 97) and 'z' (index 122) (case sensitive)

2nd Alternative [az][0-9]{2,} Match a single character present in the list below [az]

az a single character in the range between a (index 97) and z (index 122) (case sensitive)

Match a single character present in the list below [0-9]{2,}

{2,} Quantifier — Matches between 2 and unlimited times, as many times as possible, giving back as needed (greedy) 0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)

Match a single character present in the list below [0-9]* * Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)

0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)

$ asserts position at the end of a line

Demo

import re 
pattern="^([a-z]{2,}|[a-z][0-9]{2,})[0-9]*$"
if re.match(pattern,input()):
    print("Ok")
else:
    print("Not ok")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM