Regex Explanation required


My Regex:


string to match:


Why is it matching 5 digits?

I want it to match the following pattern only:
12em or 12 rem or 12.345em or 12.345rem

up to 3 decimal places only.

>Solution :

Try this one:


Here is a demo

  • ^ – assures we are at the begining of the string
  • ^\d{0,2} zero up to two decimals after begining of string
  • (\.\d{1,3}) optional group of 1 up to 3 digits with . in front
  • \s? optional whitespace character
  • r? optional r character
  • em mandatory em
  • $ end of the string

Leave a Reply Cancel reply