What is wrong with this one

Tell us what’s happening:
Describe your issue in detail here.

Your code so far


let username = "Oceans11";
let userCheck = /^[a-z][a-z]+\d*$|^[a-z][0-9][0-9]+/ig; // Change this line
let result = userCheck.test(username);

let resultVis = username.match(userCheck);

console.log(resultVis);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36.

Challenge: Restrict Possible Usernames

Link to the challenge:

Hello there.

Do you have a question?

If so, please edit your post to include it in the Tell us what’s happening section.

Learning to describe problems is an important part of learning how to code.

Also, the more information you give us, the more likely we are to be able to help.

you are missing something at the end of your regex pattern
you need a char that matches any string with a character at the end of it

hint: it’s a character that you’ve already used once already in your pattern somewhere near the middle of it

Look at the error message you are getting in the console below the editor:

“Your regex should not match the string c57bT3”

This violates the second rule:

“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.”

You are very close. I’ll give you a hint. You are missing one special character.

1 Like

You also have to be aware of how the global flag works with RegExp.prototype.test()

1 Like

@lasjorg

I’m not the OP, but since you brought that up… I just read that MDN doc out of curiosity. Good info to know about how global g affects test().

Since lastIndex is writable (as it must be, or it couldn’t be increased) how would one reset lastIndex manually, or in code if test() didn’t get a match (returned false)? or can you? or would you even want to? or is that just a side-effect of using g with test() that you have to live with?

Side Note:
“lastIndex” seems to me to be an unfortunate property name because one might infer at first glance without reading further that it means lastIndex is the item at index str.length - 1 rather than what it actually means, the index of the character in the string where it left off last time)

You can set it back to 0.

let username = "Oceans11";
let userCheck = /^[a-z][a-z]+\d*$|^[a-z][0-9][0-9]+$/gi;
let result = userCheck.test(username);
userCheck.lastIndex = 0;
1 Like

Ah! Well that’s easy enough! Thanks!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.