I need to match either of these numbers in a capturing group: 1 2 4 8 16 32 64.
I tried: ([1248\b16\b\b32\b])(.*)$
that didn't work.
let RE = /([1248163264])/
let trues = ["q1", "q2", "q4", "q16", "q32", "q64"].forEach(_ => _.match(RE))
let falses = ["q3", "q13", "q6"].forEach(_ => _.match(RE))
console.log(trues, falses)
CodePudding user response:
This pattern ([1248\b16\b\b32\b])(.*)$
has 2 capture groups, where the first capture group contains a character class that matches 1 of the listed characters.
If you meant to use a word boundary like (1248\b16\b\b32\b)
then that would also not match as this is a pattern where all has to match in that order.
There is also no word boundary between 2 digits.
You might use negative lookarounds as digit boundaries instead:
(?<!\d)(?:[1248]|16|32|64)(?!\d)
let RE = /(?<!\d)(?:[1248]|16|32|64)(?!\d)/;
["q1", "q2", "q4", "q16", "q32", "q64"].forEach(s => console.log(`${s} --> ${RE.test(s)}`));
["q3", "q13", "q6"].forEach(s => console.log(`${s} --> ${RE.test(s)}`));