Regular Expressions - Restrict Possible Usernames

I don’t have a problem with this chalenge but I assume that somebody is going to have, and because of that I just want to post some hint.

In this one you should use quantifiers. This isn’t covered before this task.
You can check how to use it here

3 Likes

I was working on this challenge. i think the solution that FFC accepts is wrong. It accepts:
/[a-zA-z]/g
However, i think its a wrong answer because, based on the rules that was given, Oceans11 should not pass the test using that expression. But it did.

I also agree with what @Saymon85 has said. You need to know about quantifiers, which was not covered. anyways, the below solution would meet all the rules yet, FCC console says it doesn’t meet the “JACK” test case, but it meets the rest.

/[a-z]{2,}\d+$|[a-z]{2,}/ig

  1. List item
    the first part after the | ensures that letter sequence with two or more characters, having numbers at the end of the word, passes the test
  2. List item
    the second part before | ensures that any letter sequence with two or more characters can pass the test {2,}.

feel free to verify all test cases in regex101.com

2 Likes

I have exactly the same problem and my code is virtually the same…

/[a-zA-Z]{2,}\d+$|[a-zA-Z]{2,}/g

I tried it on repl.it and it works just fine, so is the console bugged?

couldn’t you just leave out the part after | and instead use a * in place of the +? so

/[a-zA-Z]{2,}\d*$/g

It wont work on all the tests. check regex101.com , put in that expression and test it using the same test scenarios’. Think of the ! as an “or” statement

@Saymon85 Thanks for the advice on this, I was initially struggling and based on the tutorials on FCC couldn’t work out how to test for length, the {X} and {X,} suggestions helped.

@chinonsoebere thank you too, as I logically have worked out a solution which I think is correct, yet I can’t get it to pass the FCC tests, however using the regex101.com link proved my code is actually correct!

So for now I will just have to skip this challenge, leaving it as incomplete!

Am I allowed to post the code i’ve used as my solution on here? So you could maybe verify I got it correct?

No qualifiers needed

let userCheck = /^[a-z][a-z]+\d*$|[a-z]\d\d/i;

I think…

1 Like

You’re welcome, we are here because of that :smile:
Yes, you are allowed it is good way to get help.

Spoiler

let userCheck =/\w.\d?/g

4 Likes

I think this is a short way to do it. Anything wrong with that ?

let userCheck = /\w./g; // Change this line

cheers mchoeti

1 Like

I found a solution without the use of quantifiers.

/.\D+\d*$/

Is there anything wrong about doing it like this?

Does anyone know why the use of the ‘g’ flag in the following doesn’t work?

Code below did not pass the test:

let username = "JackOfAllTrades";
let userCheck = /\D\D\D*\d*$/ig; // Change this line
let result = userCheck.test(username);

Code that passed the tests:

let username = "JackOfAllTrades";
let userCheck = /\D\D\D*\d*$/i; // Change this line
let result = userCheck.test(username);

Check the examples section

I had a litle difficult with this challenge (at the first minutes) but i finally got it with this try

let userCheck = /^[a-zA-Z]+[a-zA-Z]+[\d]*[\d]*$|^[a-zA-Z][\d][\d]+$/;
1 Like