0

I am trying to build a regex in javascript to match a 9-digit number with these characteristics:

First 3 digits should not be ‘0’ ,

4th and 5th Digit should not be ‘0’,

Last 4 digits should not be ‘0000’,

First 3 digits should not be ‘666’,

Last 3 Digits Should not be greater than ‘899’

Can someone please help me out with this.

Here is my current regex:

/^666[^0]{3}[1-9]{2}0000$/

, but it’s not meeting the criteria

1
  • @lucumt i am thinking, that if we can match the last 3rd digit should not be 9 with the numbers followed by it. By it we can achieve that, but facing difficulty in building it with the above scnarios. Can you please help me out Commented Jul 26, 2018 at 4:44

3 Answers 3

1

Try This regular expression it will work ^(?!(000)|(666))[0-9]{3}[1-9]{2}[0-9][0-8][0-9]{2}(?<!0000)$. See demo here

Sign up to request clarification or add additional context in comments.

1 Comment

it is failing if any of the 3 digits is having 6. But it should allow 6 Eg:165456789, in this scenario it needs to accept but it is failing
1

Try this ^[^06]{3}[^0]{2}[^0][1-8][^0]{2}$

Below is my explanation for it

First 3 digits should not be ‘0’  
First 3 digits should not be ‘666’,->  ^[^06]{3} 
4th and 5th Digit should not be ‘0’, -> [^0]{2}
Last 4 digits should not be ‘0000’, -> [^0]{4}
Last 3 Digits Should not be greater than ‘899’ -> [1-8][^0]{2}$ 

3 Comments

@Inder Sorry,I just take it as Last 3 Digits Should be greater than ‘899’, I do not pay attention to not
This excludes any 6 in the first 3 digits, while the requirement was that the first 3 digits should not be 666 - not the same thing. Also, the ending [1-8][^0]{2}$ wrongfully excludes 0xx where x is anything and y00 where y is 0-8.
@CertainPerformance Thank you for your comment,I think the accepted answer has the same issue
1

Because all nine characters need to be a digit, you might use lookahead from the beginning to check that there are 9 digits followed by the end of the string, which will make the subsequent groups easier to manage. Then, you need to utilize character sets and negative lookahead. The first two conditions look to collapse together - the first five characters need to be other than 0:

/^(?=\d{9}$)(?!666)[^0]{5}(?!0000)\d[^9]/

const re = /^(?=\d{9}$)(?!666)[^0]{5}(?!0000).[^9]/;
`555555555
5555555555
055555555
555505555
666555555
555550000
555550001
555550900
555550953`
  .split('\n')
  .forEach(n => console.log(re.test(String(n))));

Explanation:

555555555   true
5555555555  false; 10 digits, not 9
055555555   false: has 0 in first 5 digits
555505555   false: has 0 in first 5 digits
666555555   false: starts with 666
555550000   false: ends with 0000
555550001   true
555550900   false: sixth digit is a 9 (so last 3 digits are 9xx, which is greater than 899)
555550953   false: same as above

https://regex101.com/r/Vpwbk0/1

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.