如何测试浏览器是否支持JS正则表达式lookahead/lookbehind?

Wil*_*llD 5 javascript regex

我有一个string.replace()使用正则表达式lookbehind 的函数。

myString.replace(/(?<!\\)'/gi, '"')
Run Code Online (Sandbox Code Playgroud)

不过,我发现目前该功能的浏览器支持有限。 https://caniuse.com/#feat=js-regexp-lookbehind

有没有办法使用javascript来测试用户的浏览器对此功能的支持?

类似于CSS.supports()函数之类的东西?

Bru*_*mos 5

这将true在 chrome 和falsesafari 上返回,没有任何语法错误:

function supportsRegexLookAheadLookBehind() {
  try {
    return (
      "hibyehihi"
        .replace(new RegExp("(?<=hi)hi", "g"), "hello")
        .replace(new RegExp("hi(?!bye)", "g"), "hey") === "hibyeheyhello"
    );
  } catch (error) {
    return false;
  }
}
Run Code Online (Sandbox Code Playgroud)