Zac
Zac

Reputation: 12836

Regular expression to match nine-digit numbers that start with a 1

I am trying to match 9-digit numbers that start with a 1

"/^1[0-9]{9}$/"

Upvotes: 2

Views: 1121

Answers (4)

Ahmad Mageed
Ahmad Mageed

Reputation: 96477

If you need 9 numbers starting with one then you only need to limit the repetition of the character class to eight (8):

"/^1[0-9]{8}$/"

In your sample you have "\ ". If you need to allow spaces use a space or \s for whitespace: [0-9 ] or [0-9\s].

Upvotes: 1

alex
alex

Reputation: 490163

/^1\d{8}/

The first number 1 will be your first digit, so the quantifier should be 8.

Upvotes: 2

tchrist
tchrist

Reputation: 80384

^(?=1)\d{9}$⁠ ⁠ ⁠ ⁠ ⁠ ⁠ ⁠ ⁠ ⁠ ⁠ ⁠ ⁠ ⁠ ⁠ ⁠ 

Upvotes: 2

BeemerGuy
BeemerGuy

Reputation: 8269

The space is \s

Upvotes: 1

Related Questions