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 
 }
asked Jun 14, 2013 at 19:20
\$\endgroup\$
1
  • \$\begingroup\$ What about 1111? \$\endgroup\$ Commented 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.

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.