Regex works on other testers but not in code

Tell us what’s happening:
I’m trying to create an passing regex and tried using a generator page for help.
https://regexr.com/ says my test pass but it doesn’t pass in your code.
Shouldn’t it work in both?

Your code so far


let username = "JackOfAllTrades";
let userCheck = /^\D{1,}[a-zA-Z]*\d*$/g; // Change this line /^\D{1,}\w\d*$/
let result = userCheck.test(username);

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0.

Challenge: Restrict Possible Usernames

Link to the challenge:

Hello there,

This is a known issue, and you can read about it here:
global flag in regular expression fails test · Issue #37890 · freeCodeCamp/freeCodeCamp (github.com)
And
Regular Expressions: Restrict Possible Usernames - Should lastIndex be reset? · Issue #40211 · freeCodeCamp/freeCodeCamp (github.com)

Essentially, the way the tests run do not work with the g flag.

That being said, your regex should still fail at least 1 test.

Hope this helps

let username = "JackOfAllTrades";
let userCheck = /^\D{2,}\d*$/; // Change this line /^\D{2,}\d*$/
let result = userCheck.test(username);

Passes every test accept Z97.

How am I to get a certificate if this can’t be completed?

This can be completed, but your Regex is incorrect. So, it will fail the tests.

Based on your last reply:

let userCheck = /^\D{2,}\d*$/;

Can you see why it fails the test?

Not a clue, I’ve spent hours searching the regex tutorials and it still makes little sense. Recursion was easier to learn.
Is the test function passing partial or full match?

only the projects are mandatory, anyway

your regex says that the string must start with two non-numbers

instead it can also start with 1 non-number and Z97 is valid - you need to change your regex

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