Can I use
\d\d\d\d[^\d]
to check for four consecutive digits?
For example,
OK:
4111124555531200003f44443g555533333
No:
f444245553f4444g4444f44444444
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.