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
}
-
\$\begingroup\$ What about 1111? \$\endgroup\$SylvainD– SylvainD2013年06月15日 07:05:15 +00:00Commented Jun 15, 2013 at 7:05
1 Answer 1
Why not simply test the valids?
if((/0110|0100|0010/).test(precode))
Seems more readable to me.
-
\$\begingroup\$ Yeah that is true, I guess I was just trying to be minimalistic. \$\endgroup\$Sanpopo– Sanpopo2013年06月15日 00:21:33 +00:00Commented Jun 15, 2013 at 0:21