Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

One way to restrict regex from going back beyond a certain pattern is by using a positive lookahead assertion. For example, if you want to match a string that contains "foo" followed by "bar", but only if "foo" appears before "baz", you can use the following regex:

/foo(?=.*baz).*bar/

This regex uses a positive lookahead assertion (?=.*baz) to ensure that "baz" appears after "foo" in the string. This means that the regex engine will only start matching "bar" after it has matched "foo" and ensured that "baz" appears later in the string.