Restrict Possible Username

Hi can any one explain me this regex: /^a-z$/i. I have some questions: Why \d\d is not same as \d*. If i took [a-z] after | the solution is still correct. Why? Why \d\d refers to last two digits why not first 2 digits or in between?

Welcome, ayaz.

Here is a tool I have found to be extremely helpful, when it comes to regex: regex101

To answer some of your questions:
/^a-z$/i This matches a at the start of the string, followed by -, ending with z. Case insensitive.

\d\d matches two digits next to each other.
\d* matches zero or more digits next to each other.

I do not understand what you mean by the next question…| is the or operator.

If you have a global identifier g, then \d\d matches every case of two digits next to each other. If you do not have the global identifier, then \d\d will only match the first two digits.

I hope this helps.

1 Like