Arsalan
Arsalan

Reputation: 744

Regular Expression exact count of characters

THIS IS NOT A DUPLICATION OF REFERENCED QUESTION because I already google this and test these solutions and after not founding appropriate answer I post this question

I need to validate some inputs that are like this:

first char always : 0

second char always: 1

and exactly 9 digits after that

e.g.

case 1:01123456789 => should match

case 2:`01123456789`0=> should not match but does match

case 3:123`01123456789`5985 => should not match but does match

I already using this pattern

/([0][1][0-9]{9})/g

but case 2 and 3 does match with this pattern too and only case 1 should be accepted

how can I achieve this?

Using this tool to test the patterns

Upvotes: 1

Views: 197

Answers (1)

Sean Bright
Sean Bright

Reputation: 120714

You need to use appropriate anchoring (\b in this case for 'word boundary'):

/\b01\d{9}\b/g

Upvotes: 3

Related Questions