Antti Kolehmainen
Antti Kolehmainen

Reputation: 1081

Multiple max lengths in a regular expression

I have the following regular expression:

[0-9]{7}-[0-9]{1}$

I should be able to match the following patterns:

1234567-8
3142539-1

But not the following:

12345678-1
1234567-12

Currently my regex matches 12345678-1 but not 1234567-12 (in JavaScript). Both should fail. What am I doing wrong?

Upvotes: 1

Views: 248

Answers (1)

Anirudha
Anirudha

Reputation: 32787

Your pattern would match any string that ends($) with [0-9]{7}-[0-9]{1} and so it would match those inputs..

Use ^(start of the string) to specify that you want to match exactly..

^[0-9]{7}-[0-9]{1}$

Upvotes: 2

Related Questions