Javascript Regex: how to simulate "match without capture" behavior of
positive lookbehind?
I have a relatively simple regex problem - I need to match specific words
in a string, if they are entire words or a prefix. With word boundaries,
it would look something like this:
\b(word1|word2|prefix1|prefix2)
However, I can't use the word boundary condition because some words may
start with odd characters, e.g. .999
My solution was to look for whitespace or starting token for these odd cases.
(\b|^|\s)(word1|word2|prefix1|prefix2)
Now words like .999 will still get matched correctly, BUT it also captures
the whitespace preceding the matched words/prefixes. For my purposes, I
can't have it capture the whitespace.
Positive lookbehinds seem to solve this, but javascript doesn't support
them. Is there some other way I can get the same behavior to solve this
problem?
No comments:
Post a Comment