Username Exercise RegExp


I am aware of other topics related to this exercise, but I have checked all of them and they do not help me understand what I am doing wrong.

I have been stuck for more than 3 hours in this exercise:

[](http://Exercise I am stuck at)

This is what my idea was, but it is not working as expected:

let username = "JackOfAllTrades";
let userCheck = /^[a-z]+[a-z]+\d*$/gi; // Change this line
let result = userCheck.test(username);

After checking the topics, I considered this following, but is not working either:

let username = "JackOfAllTrades";
let userCheck = /^[a-z]{2,}\d+$|[a-z]{2,}/gi;
let result = userCheck.test(username);

Thanks to any help provided.

your regex is good but the way ‘test’ works is weird…
I changed your code to:

let username = “JackOfAllTrades”;
let userCheck = /^[a-z]+[a-z]+\d*$/i; // Change this line
let result = username.match(userCheck);

and that worked for me
(changed test to match and removed the global match)

edit: i think the reason that ‘g’ makes things not work is because with each successive call to match or test, the command tries to find the next match (starting not from the beginning of the string but from where the end of the last match was…)

1 Like

MDN on RegExp

Thank you! About the g, then if I use g, I can only have “one condition”? For example:

/[a-e]/g would search for a, b, c,d,e from the beginning to the end of the string, but if I had /[a-e][2]/g, it would only take account the first section because before the second section starts, the string would be over?

This challenge has a very loose set of tests that it will accept many loopholes.

The strict version that conforms to every single constraint is this

1 Like

i’m sorry I can’t answer this. I still don’t really know why your original regex was not working.

The only case that your original regex fails is when you get a string like ‘s39’ which should pass, but your regex fails to recognize it as valid. But that is not part of the test suite that FCC gives.

Sorry but that’s all I know.

1 Like

Except you have to account for them if they are part of the requirement. The strictness of regex depends on where you are using it for, but in general neglecting spec can lead you to unexpected bugs and security issues.

Even though these requirements are silly for a username, if this was used for validating username, then it is better to be strict about it.

If your regex was used with regex.test(), so many cases would be false positive. If it was used with regex.match(), it won’t capture enough characters even the valid ones.

So, it definitely can’t serve as a choke point to validate username.

oh, you’re totally right.
i wasn’t thinking there could be letters after the numbers or two character usernames with numbers.
will delete for clarity.


reading into the rules, i came up with this:


this is implying:

'a' returns false
'1' returns false
'a1' returns true
'aa' returns true
'1a' returns false
'aa1' returns false because “a two letter username can only use alphabet letter characters”
'aaa1' returns true
'a11' returns true
'aaa' returns true
'111' returns true

using the test method.

That’s overly long imo. I’d rather parse it than use regex at that point.

This is simpler one. See if you can break this

1 Like

'ab123' would return true with your regex .
as per the last rule:

A two-letter username can only use alphabet letter characters.

'ab123' is a two letter username that uses numbers, and is breaking that rule.

Two letter username is a username length of 2. This means when the length of username is two, all the characters must be alphabet.

1 Like

i mean, it’s a typo, correct?
if two-letter was two-character i would agree with you, and your regex would work.

but my thoughts are:
it would be impossible for a two-letter username to have anything other than alphabet letter characters so i’m interpreting the rule as a technical parameter rather than an obvious clarification.

If that constraint existed independently, then it might be ambiguous but

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

This sounds more or less like:
“Username must be at least two characters, but if a username is exactly two characters, then all the characters must be alphabets.”

But whatever.

1 Like

Am I missing an edge case with the following?


Isn’t a123 a counter example?

Also, I think the $ at the end should be mandatory if the regex is served as a choke point.

1 Like