简体   繁体   中英

How can I write the Javascript Regular Expression pattern to handle these conditions

In My exercise, I'm given a task to write a regular expression pattern that validates username input for storage into database.

Usernames can only use alpha-numeric characters.

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.

Username letters can be lowercase and uppercase.

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

I succeeded to pass all tests except one

A1 is not supposed to match the patern

let userCheck = /^[A-Za-z]+\w\d*$/;
let result = userCheck.test(username);

Edit:

My first answer did not fully solve the task. This regex does:

^([A-Za-z]{2}|[A-Za-z]\w{2,})$

it matches either two characters, or one character followed by at least two characters and/or digits ( \w == [A-Za-z0-9] ). See the demo here: https://regex101.com/r/sh6UpX/1

First answer (incorrect)

This works for your description:

let userCheck = /^[A-Za-z]{2,}\d*$/;
let result = userCheck.test(username);

Let me explain what went wrong in your regex:

/^[A-Za-z]+\w\d*$/

You correctly match, that the first character is only a letter. The '+' however only ensures, that it is matched at least one time. If you want to match something an exact number of times, you can append '{x}' to your match-case. If you rather want to match a minimum and maximum amount of times, you can append '{min, max}'. In your case, you only have a lower limit (2 times), so the max stays empty and means unlimited times: {2,}

After your [2 - unlimited] letters, you want to have [0 - unlimited] numbers. '\w' also matches letters, so we can just remove it. The end of your regex was correct, as '\d' matches any digit, and '*' quantifies the previous match for the range [0 - unlimited].

I recommend using regex101.com for testing and developing regex patterns. You can test your strings and get very good documentation and explanation about all the tags. I added my regex with some example strings to this link: https://regex101.com/r/qPmwhG/1

The strings that match will be highlighted, the others stay without highlighting.

You can use an alternation after ^[az] the first letter to require either [az]+ one or more letters followed by \d* any amount of digits | OR \d{2,} two or more digits up to $ end of the string.

let userCheck = /^[a-z](?:[a-z]+\d*|\d{2,})$/i;

See this demo at regex101 - Used with the i -flag (ignore case) to shorten [A-Za-z] to [az] .


PS : Just updated my answer at some late cup of coffee ☕. Had previously misread the question and removed my answer in meanwhile. I would also have missed the part with eg Z97 which I just read at the other answers comments. It's much more of a challenge than at first glance... obviously:)

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