0

i have a regexes in a javascript object like so

let regexes = {
            numeric: new RegExp("^[0-9]*$"),
            hex: new RegExp("[0-9a-fA-F]+")
        };

when I do regexes.hex.test("abc123") it works like its supposed to

same with regexes.hex.test("123abc")

however if I do regexes.hex.test("123aby"), it passes when it shouldn't

here is me playing around with the console.

const regex =new RegExp("[0-9a-fA-F]+")
undefined
regex.test('1234567890gbcdef')
true
const regex2 = RegExp("[0-9a-f]+")
undefined
regex2.test('1234567890gbcdef')
true
regex2.test('1234567890ghcdef')
true
regex2.test('1234567890ghijkl')
true
regex2.test('ghijkl')
false
regex2.test('123ghijkl')

  • 1
    `[0-9a-fA-F]+` is simply looking for any of those chars so it succeeded by just matching the `1` from `123aby` – MonkeyZeus Aug 27 '19 at 19:59
  • You need anchors to ensure exactly 6 hexadecimal chars `^[0-9a-fA-F]{6}$`. See https://regex101.com/r/NLhrHq/1 – MonkeyZeus Aug 27 '19 at 19:59
  • `it passes when it shouldn't` you have to explain _shouldn't_ because like _NOT_ it's a whole lotta stuff ... –  Aug 28 '19 at 00:56

0 Answers0