gpt4 book ai didi

javascript - 如何测试确定浏览器是否支持 JS regex 前瞻/后视?

转载 作者:行者123 更新时间:2023-12-04 17:30:20 27 4
gpt4 key购买 nike

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

myString.replace(/(?<!\\)'/gi, '"')

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

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

类似于 CSS.supports() 函数?

最佳答案

这将在 chrome 上返回 true,在 safari 上返回 false,没有任何 SyntaxError:

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

关于javascript - 如何测试确定浏览器是否支持 JS regex 前瞻/后视?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60325323/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com