Can anybody break down this Regular Expressions line for me please,

Can anybody break down this Regular Expressions line for me please,?

/^[a-z][a-z]+\d*$|^[a-z]\d\d+$/ig

It’s from Regular Expressions - Restrict Possible Usernames.

For some reason I’m not quite getting this.

Thanks,

Stvee

1 Like

I’ll give this a shot - but I also suggest plugging it into regexr for a full breakdown.

  • / ^[a-z] [a-z]+\d*$|^[a-z]\d\d+$/ig means match if the string starts (^) with one letter (case insensitive because if the i flag at the end)
  • /^[a-z] [a-z]+ \d*$|^[a-z]\d\d+$/ig means match if the string’s next one or more (+) characters are letters (case insensitive because if the i flag at the end)
  • /^[a-z][a-z]+ \d*$ |^[a-z]\d\d+$/ig means match if the string ends ($) with zero of more (*) digits (\d)
  • /^[a-z][a-z]+\d*$ | ^[a-z]\d\d+$/ig means match if the string matches either of the regular expressions before of after the |
  • /^[a-z][a-z]+\d*$| ^[a-z] \d\d+$/ig means match if the string starts (^) with one letter (case insensitive because if the i flag at the end)
  • /^[a-z][a-z]+\d*$|^[a-z] \d \d+$/ig means match if the next character is a digit (\d)
  • /^[a-z][a-z]+\d*$|^[a-z]\d \d+$ /ig means match if the string ends ($) with one or more (+) digits (\d)

Really the first regex (^[a-z][a-z]+\d*$) matches all strings that start with at least two letters and end with any number of digits (including no digits), then the second regex (^[a-z]\d\d+$) matches all strings that start with exactly one letter and end with at least two digits.

Hope that helps and sorry if I missed anything!

(edited to fix a typo & formatting)

2 Likes

Thank you for your reply, Lucas.

To be honest I think I had just about figured it out, I think you know a lot more than me though.

Thanks.

Steve

Hi again Lucas,

I have just looked through your answer again, don’t totally get it yet but that is the best help I could ever ask for. Cheers.

Steve

Happy to help! If there is something specific you still have a question about let me know.

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