Phelineg
Phelineg

Reputation: 59

Regular expression for hexadecimal string

I am trying to create a regex that detect if a string of hexadecimal is only a combination of 00 , 06, 03 and space.

The closest i've found so far is ^(00|06|03)$ but it's still giving me false for 0300

0300 will match
0600 0300 match
0612 0300 no match
3030 no match

Upvotes: 0

Views: 72

Answers (2)

Rafael
Rafael

Reputation: 7746

Close, but you need closure and some safe-guarding:

let v = /^((00|06|03){2}\s)*(00|06|03){2}$/;

[
  '0300',
  '0600 0300',
  '0612 0300',
  '3030',
  '0303 0000',
  '0630 0300',
  '8790 0060',
  '03 0000',
  '0003 0006 0000 0000 0303 0606 0600 0306 0000',
  '0606 0603 0303'
].map(s => console.log("%s : %s", s, v.test(s)));

Upvotes: 0

Adeel Khan
Adeel Khan

Reputation: 11

^(00|06|03)$ will only match '00' or '06' or '03'. If you're expecting this combination to repeat, you need to add +.

Try ^(00|06|03| )+$

I've included the space in there as well. This will match your scenarios.

Upvotes: 1

Related Questions