2

I'm trying to build a regular expression to match exactly this kind of format:

40XXXXXX 41XXXXXX 43XXXXXX

So far i have this \d{2}(^40?|^41?|^43?)\d{6}

But it doesn't work, i've spend hours in http://regexr.com/ trying to make it work without luck

Appreciate your help

Douglas Roos
  • 533
  • 8
  • 26
  • I don't understand why you used `\d{2}` at the beginning of your regex. Also, you need to match `$` the end of string. `^4[013]\d{6}$` – Mariano Nov 19 '15 at 10:28
  • Possible duplicate of [Reference - What does this regex mean?](http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) – Mariano Nov 19 '15 at 10:30

1 Answers1

2

Use following regex

/^4(0|1|3)[0-9]{6}$/

Regex Demo

Or,

/^4[013][0-9]{6}$/

Demo

Explanation:

  1. ^4: Start with 4
  2. (0|1|3): Matches 0 or 1 or 3. | is OR in regex
  3. [013]: Matches any one digit from the character class
  4. [0-9]{6}: Matches any number from 0 to 9 exactly six times
Tushar
  • 78,625
  • 15
  • 134
  • 154