Regex to match a constant number within a number in HTML

Viewed 339

I'm trying to write a regex in an html pattern attribute to match the input(type="number")

  1. The length should be 16 digits
  2. The first 5 digits should be exactly 11995

I've tried ([11995]+[0-9]) but it's not working.

Any help with this will be highly appreciated! Thanks.

2 Answers

A sequence of numbers when written inside a square brackets means a single character and any character contained within those square brackets. Hence, [11995] means either 1 or 9 or 5. The correct way to write that regex would be this,

\b11995[0-9]{11}\b in case you want to match such numbers in larger text

OR

^11995[0-9]{11}$ in case you want to match the number as whole.

11995\d{11} will match 11995, and then 11 more digits afterwards. \b11995\d{11}\b asserts it is followed on both sides by a "word boundary", and ^11995\d{11}$ asserts it is the only thing in the string. These can be easily modified to match other delimiters, such as " or <>.

Try it here!

Related