Can anybody tell me what’s wrong with my Regex? It passes all tests but one: “Your regex should not match 007”
let username = "JackOfAllTrades";
let userCheck = /[\w]{2}\d*$/i;
let result = userCheck.test(username);
Can anybody tell me what’s wrong with my Regex? It passes all tests but one: “Your regex should not match 007”
let username = "JackOfAllTrades";
let userCheck = /[\w]{2}\d*$/i;
let result = userCheck.test(username);
[\w]{2} means exactly 2 of any word character (ie, a-z, A-Z, 0-9)
\d*$ means 0 or more of any digit characters at the end of the string
So putting those together, your RegEx will match any strings, as long as they have exactly 2 word characters at the beginning, and 0 or more digits at the end.
Here are a few pieces that might help you build your RegEx:
[a-z] means any character from a to z
\D means any non-digit character
{2,} means 2 or more
^ means at the start of the string
\w includes letter and digits as well. so for [\w]{2} the 00 matches. also \d means digit for 7, so 007 matches.
Please share the case-study link also so we can help better.
Thanks a lot for your help! It worked! Cheers! <3
Thanks for your help!!!
i passed with the answer /\D./
\D stands for non-digit character and I used “.” as there are no restriction for the ending.
let username = “JackOfAllTrades”;
let usercheck = /[a-z]{2}\d*$/i
Useename atleast 2 characters only long not characters and number
I used /^[\D](\D+\d*|\d\d+)$/
Therefore 1 at the start and (2 OR 3) at the ending
This is my understanding of the problem. It’s very tricky.
The summary of the requirements are:
Tricky requirements:
With these in mind, we can conclude that a letter should be the first character:
/\D
(length covered = 1)
If the username has no number in it (at least two character up to unknown length), we could use:
/\D+\D
(length covered = 1 + n + 1)
I needed the 2nd \D
to close the length and to not leave the regex open-ended (and possibly 1 length only if n is 0) with “+”.
If the username has a number, it should be at the end only.
/\D+\d
(length = 1 + n + 1)
This will pass any username with a bunch of letters and least a number at the end.
But it will not pass the two-character requirement, e.g. “A1”.
Tip: Modify the \d
a little further that will pass “A11” but not “A1”. Don’t forget to use the OR
operator as well.