Hey there I'm trying to write a multi-word RegEx that uses word boundaries. The string I'm searching is as follows (this is only a test string):
const regString = "/gamma/ truck?timer!doctor\\face"
Here is my regular expression:
const testReg2 = new RegExp("\\b\/gamma/|truck|\\?|face\\b", "gi");
For whatever reason the console posts ['truck', '?', 'face']
but it refuses to find '/gamma/' which is puzzling because const testReg = new RegExp("/gamma/");
does find '/gamma/'.
const regString = "/gamma/ truck?timer!doctor\\face"
const testReg2 = new RegExp("\\b\/gamma/|truck|\\?|face\\b", "gi");
console.log(regString.match(testReg2))
CodePudding user response:
The issue is in using \b\/gamma
and expecting it to match "/gamma"
(same for the closing \b
).
The Word Boundary \b
in order to work it should be followed (or preceded, depending on where it's used) by at least one (or more) Word characters. Which is not — due to the non-word /
character.
To visualize, let's spread those characters:
# Regex: \bxyz
/ g a m m a
^--------- \b points here. Match: `gamma`
# Regex: \b\/xyz
/ g a m m a
\b points nowhere. Match: none since `/` is a non-word char
Either: remove the \/
since that's already a specific character prefix / suffix, or...
If you really need to make sure the exact existance and sequence of those characters /gamma/
, than don't use \b
for the /gamma/ case.
\/gamma\/|\b(truck|\?|face)\b
const regString = "/gamma/ truck?timer!doctor\\face"
console.log(regString.match(/\/gamma\/|\b(truck|\?|face)\b/gi))
CodePudding user response:
According to this answer
[...] at the beginning or end of a string if it begins or ends (respectively) with a word character ([0-9A-Za-z_])
Your string doesn't start with [0-9A-Za-z_]
hence not match