2
\$\begingroup\$

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 
 }
\$\endgroup\$
1
  • \$\begingroup\$ What about 1111? \$\endgroup\$
    – SylvainD
    Jun 15, 2013 at 7:05

1 Answer 1

2
\$\begingroup\$

Why not simply test the valids?

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

Seems more readable to me.

\$\endgroup\$
1
  • \$\begingroup\$ Yeah that is true, I guess I was just trying to be minimalistic. \$\endgroup\$
    – Sanpopo
    Jun 15, 2013 at 0:21

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.