Regular Expression

Watch out for pitfalls - Safari and positive lookbehind assertions

regular expression # 1. Safari handles positive lookbehind assertions in regular expressions, denoted by (?<=…), abnormally # js Copy 1 2 3 4 5 6 // The following code works fine in Chrome but throws an error in Safari. "https://linlccc.com?theme=light".replace(/(?<=[?|&]theme=)\w+/, "dark"); // The solution is to use capturing groups and string replacement. "https://linlccc.com?theme=light".replace(/([?|&]theme=)\w+/, "$1dark"); // Both of the execution results above are "https://linlccc.com?theme=dark"

2023-02-24 · 1 min · 63 words · Linlccc