Given a regular expression, I can easily decide where to start looking for a match from in a string using lastIndex.
Now, I want to make sure that the match I get doesn't go past a certain point in the string.
I would happily enclose the regular expression in a non-capturing group and append, for instance, (?<=^.{0,8}).
But how can I achieve the same goal without lookbehinds, that still aren't globally supported?
Note:
- While it might be the only reasonable fallback, slicing the string is not a good option as it results in a loss of context for the search.
Example
https://regex101.com/r/7bWtSW/1
with the base regular expression that:
- matches the letter 'a', at least once and as many times as possible
- as long as an 'X' comes later
We can see that we can achieve our goal with a lookbehind: we still get a match, shorter.
However, if we sliced the string, we would lose the match (because the lookahead in the base regular expression would fail).
Your pattern in the regex demo
(?:a+(?=.*X))(?<=^.{0,4})uses a lookbehind assertion with that can yield multiple separate matches.See a regex demo for the same pattern with multiple matches in the same string
Without using a lookbehind, you can not get those separate matches.
What you might do is use an extra step to get all the matches for consecutive
achar over matched part that fulfills the length restriction (In this case the group 1 value)The pattern matches
^Start of string(Capture group 1[^\nX]{0,3}aMatch 0-3 times a char other than a newline or X and then matcha)Close group 1[^\nX]*XMatch optional chars other than a newline orXand then matchXRegex demo