9

Can I use

\d\d\d\d[^\d]

to check for four consecutive digits?

For example,

OK:

  • 411112
  • 455553
  • 1200003
  • f44443
  • g55553
  • 3333

No:

  • f4442
  • 45553
  • f4444g4444
  • f44444444
1
  • Is the second to last example correct? Should the regex not match a string that has multiple series of 4 consecutive digits?
    – Daan
    Apr 24, 2012 at 11:11

3 Answers 3

21

If you want to find any series of 4 digits in a string /\d\d\d\d/ or /\d{4}/ will do. If you want to find a series of exactly 4 digits, use /[^\d]\d{4}[^\d]/. If the string should simply contain 4 consecutive digits use /^\d{4}$/.

Edit: I think you want to find 4 of the same digits, you need a backreference for that. /(\d)\1{3}/ is probably what you're looking for.

Edit 2: /(^|(.)(?!\2))(\d)\3{3}(?!\3)/ will only match strings with exactly 4 of the same consecutive digits.

The first group matches the start of the string or any character. Then there's a negative look-ahead that uses the first group to ensure that the following characters don't match the first character, if any. The third group matches any digit, which is then repeated 3 times with a backreference to group 3. Finally there's a look-ahead that ensures that the following character doesn't match the series of consecutive digits.

This sort of stuff is difficult to do in javascript because you don't have things like forward references and look-behind.

7
  • What if i want to flag out any part of the string contains 4 digits, e.g. 411114 or 11114 or 41111
    – HJW
    Apr 24, 2012 at 8:59
  • This works as expected. Thank you. what does backreference mean?
    – HJW
    Apr 24, 2012 at 9:08
  • The \1 in the regex references the first group in regex ((\d)). Read more about them here: regular-expressions.info/brackets.html. This will also match strings like "41111114", though. Is that what you want?
    – Daan
    Apr 24, 2012 at 9:12
  • I don't want to match 41111114 because there are more than 4 "1"s
    – HJW
    Apr 24, 2012 at 9:18
  • 2
    Btw how did you come up with this REGEX, it seems complicated, can you explain? :P
    – HJW
    Apr 24, 2012 at 10:18
4

Should the numbers be part of a string, or do you want only the four numbers. In the later case, the regexp should be ^\d{4}$. The ^ marks the beginning of the string, $ the end. That makes sure, that only four numbers are valid, and nothing before or after that.

1

That should match four digits (\d\d\d\d) followed by a non digit character ([^\d]). If you just want to match any four digits, you should used \d\d\d\d or \d{4}. If you want to make sure that the string contains just four consecutive digits, use ^\d{4}$. The ^ will instruct the regex engine to start matching at the beginning of the string while the $ will instruct the regex engine to stop matching at the end of the string.

3
  • ^\d{4}$ doesn't seem to match 4 consecutive numbers
    – HJW
    Apr 24, 2012 at 8:57
  • \d{4} seem to allow 51111115, can i disallow this?
    – HJW
    Apr 24, 2012 at 8:58
  • @ChinBoon: The first regex should match exclusively 4 digits. Are you sure you are using the Javascript syntax correctly? It should be something like so: /^\d{4}$/
    – npinti
    Apr 24, 2012 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.