-1

I need a regular expression to match exact 11 digits(no spaces, no alphabets). Valid values are

  1. 12345678987 - Valid
  2. 123456789876 - not valid because it has more than 11 digits
  3. 1234 5678987 - Not valid (since it has space)
  4. 11223344556 - valid
  5. A1234B5678987 - not valid (because it has alphabets)

I tried the below expression but no luck var aa = new RegExp("^[0-9]{11}); var bb = new RegExp("/d{11});( this will give valid to even 1,2,3,4)

Suhas R
  • 1
  • 2
  • Your last paragraph has several syntax errors (non-matched quotes) and a wrong slash in the code. – trincot Oct 12 '20 at 08:08

1 Answers1

1

Use start and end of string

^\d{11}$

const str = `12345678987
123456789876
1234 5678987
11223344556
A1234B5678987`;

console.log(str.match(/^\d{11}$/gm))

or using a Word Boundary:

\b\d{11}\b
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313