Nishant Nishant - 1 year ago 46
Javascript Question

Regex JavaScript : Need to match numbers in the format A12.12.122 , C1.20.23

/^[a-zA-Z]\d+\\.?\d+?\\.?\d+$/g


I have written this pattern but it doesn't match a single character followed by a single or two digits. It matches only when there 3 or more digits followed by a digit.
Tried groups but still didn't work

/^[a-zA-Z]\d+(\\.?\d+?(\\.?\d+))$/g


Is there a better way to write this pattern?

It should match all the alphanumeric characters for example A12, A12.25, F12.23.36, G45.36, H475. Please can anyone help with this.

Answer Source

You can do:

^[a-zA-Z]\d+(\.\d+)*$
  • ^ matches start of the line

  • [a-zA-Z] matches any letters from a to z, in any case

  • \d+ matches any digit one or more time

  • (\.\d+)* matches a . followed by one or more digits, zero or more times, at the end of the line ($)

Demo