Regex in javascript - match string like "ABC12"

How to match next line using regex in javascript?

  • Has a total of 5 characters
  • The first three characters are capital letters
  • The last 2 characters are just numbers

I have this template, [AZ]{3}[0-9]{2} , but it seems that it is still missing something. Thanks for the help.

+6
source share
1 answer

You will also need anchors :

 var regexp = /^[AZ]{3}[0-9]{2}$/ 

Otherwise, the substrings will also match (for example, ABC12 inside xyzABC1234 ).

  • ^ means "start of line"
  • $ means end of line
+7
source

Source: https://habr.com/ru/post/927912/


All Articles