Restrict Possible Usernames - Why is caret used in the help guide?

Please see

I have been able to use

/[a-z]{2,}\d*$/i

To solve this challenge.

However the guide states

^/[a-z]{2,}\d*$/i

Why the ^?

For condition 1 numbers have to be at the end so this can be met using \d*$

For condition 2 case does not matter hence use of i after /

For condition 3 (which has 2 conditions inside…)

Part 1 - at least two characters long. This is met by using {2,}

Part 2 - 2 letter usernames must use alphabet characters. Surely this means [a-z] to me?

Doesn’t ^[a-z] mean anything that is NOT [a-z]?

Your code so far


let username = "JackOfAllTrades";
let userCheck = /[a-z]{2,}\d*/i;


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36.

Link to the challenge:

@NiSofwareEngineer ^ checks for something at the start/beginning of the string. I agree that it’s not necessary to solve this.

I thought ^ is a negation so for example, ^a would mean anything that is NOT a?

If it is not negation then is there a symbol that does implement negation?

@NiSofwareEngineer Ah, yes. If the ^ is within brackets, like [^a] then it’s not a. If it’s not within brackets, like in the guide example:

/^[a-z]{2,}\d*$/i

it means match a-z only at the start of the string.

Does that make sense? I’m using regex101.com to research this.

2 Likes

Oh thank you. That’s where I got confused. Odd to see the same symbol acting completely different depending on where it’s placed…

Just to double check my new learnt stuff

[^\d] means anything not a digit/number therefore

[^\d] = [\D]

Is it fair to say this?