Take the tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have a string of four bits which represent true/false values. There are only seven valid options:

1010
1110
0000
1101
1001
1000
0101

There are three options which could potentially be selected which are not valid and that I want to check for before I proceed with some other code. These are:

0110
0100
0010

I want to do this with as little code as possible thus having one regex to test all three conditions. My question is if this is a correct regex to accomplish this test. It seems to work, but I am not a regex expert, and have to be sure in this case.

 if (!/0(10|01|11)0/.test(precode)) {
     //do some code 
 }
share|improve this question
 
What about 1111? –  Josay Jun 15 '13 at 7:05
add comment

1 Answer

up vote 2 down vote accepted

Why not simply test the valids?

if((/0110|0100|0010/).test(precode))

Seems more readable to me.

share|improve this answer
 
Yeah that is true, I guess I was just trying to be minimalistic. –  Sanpopo Jun 15 '13 at 0:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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