I use Repl.it to check my code and the following code appears to work, however, when plugging this code into the interface at Regular Expressions: Restrict Possible Usernames, the solution is not valid.
let username = "JackOfAllTrades";
let userCheck = /^[a-z][\d|a-z][\d|a-z]*$/i;
let result = userCheck.test(username);
Code Explanation
/^[a-z] = ‘/’ begins the regex, ‘^’ anchors first character, first character ‘[a-z]’ must be letter character a-z.
[\d|a-z] = the constraints of second character ‘[]’ are number ‘\d’ 0-9 OR ‘|’ letter character ‘a-z’
[\d|a-z]*$ = the constraints of third character ‘[]’ are number ‘\d’ 0-9 OR ‘|’ letter character ‘a-z’ that can occur zero or more times ‘*’ and are is anchored exclusively to the end of the string ‘$’
/i = ‘/’ ends the regex matching, and ‘i’ indicates the regex is not case sensitive
Thanks RandellDawson, clear description to what went wrong. Much appreciated
*4) Usernames have to be at least two characters long. A two-character username can only use alphabet letters as characters.
@nedious has basically hard coded his solution, if you will add just 1 more test, he will just include that exception. What you guys need is a function that will generate 500-1000 random usernames, so that’s impossible to hard code
Reading through your response @snigo helped identify a pattern of ‘hard coding’ I had been engaging in. Grateful for you highlighting this, I was able to go back and rework my solution.
I believe the following is not ‘hard coded’
let userCheck = /^([a-z]+)([a-z]+|[0-9][0-9]+)$/i; // Change this line
More thanks to @camperextraordinaire and @ILM for investing time in this thread! I learned something new
Oh jeez, yes in the light of day I remember there are characters that exist besides letters and numbers (+, =, \ , ?..etc) As those weren’t part of the test cases I didn’t even consider them!
You’re correct as well that ‘A007’ fails, I will give another shot. Thank you for the feedback I am really struggling with regex.
Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.
Solution 2 provided does not pass the username A2g .The instruction mentions - “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.” It does not disqualify usernames with numbers in between. How then is the Solution 2 one of the possible solutions?
@camperextraordinaire@snigo I think one more test case needs to be added to make the evaluation of the test foolproof. Because I came up with another regex which passes but is not correct -----> ^\D(\d{2,}|[a-z]+\d+)$
This regex matches the username %B but the instruction 4 requires - A two-character username can only use alphabet letters as characters. It also passes the evaluation currently.