Search code examples
regexvisual-studio-codereplaceregex-lookarounds

Why is `f(?=\=)` invailid regex in VSCode?


In VSCode if I use f(?=\=) in the regex find and replace I get an error that says:

Invalid regular expression: /f(?=\=)/: Invalid escape

however, if I do f(?==) it works fine. I don't see any issue with the first regex, its just a specifying the literal = sign as a positive lookahead. Why can you not escape an = sign?


Solution

  • In your regex, (?= starts a positive lookahead, but your second = is the part being matched, not part of that specific syntax. So the slash is interpreted as an escape, and an escaped = sign is invalid.