Need Regex Help

I am currently on this topic - " Regular Expressions: Restrict Possible Usernames"
And i am quiet confused .


let userCheck = /[a-zA-Z]\D+\d*/; // Change this line

let result = userCheck.test(username);

But the console says

Your regex should not match BadUs3rnam3

How come my regex pattern which defines first position as a alphabet and the following with non digits match the word. \D+ is there so it should only match upto BadUs3 but why is the word matched :frowning:

That’s correct but your regex then starts again and matches the rest of the string. If you restrict your regex with position markers, the solution works for this particular case.

/^[a-zA-Z]\D+\d*$/

^ meaning the beginning, $ meaning the end of your string. But this regex will not fully work either since β€œZ97” should be allowed but your \D+ prevents that ;-).

Why come the regex start again :face_with_monocle: , /D+ should end after it has got the digit right? What concept am I missing

if you want for the regex to match the whole string and not just parts of it you need to specify it using the start of string and end of string symbols

Please help me understand from the basic interpreter algorithm , i am confused about this regex stuff

something simple:

/a/; // matches a string that contains the character "a" like "a", "almond", "teacup", "gorilla"
/a$/; // matches a string that ends with the character "a", like "a", "gorilla"
/^a/; // matches a string that starts with the character "a", like "a", "almond"
/^a$/; // matches a string that exactly is that pattern, just the string "a"
1 Like