如何测试以确定浏览器是否支持JS正则表达式前瞻/后视?

问题描述 投票:1回答:1

我有一个string.replace()函数,该函数使用正则表达式向后移动。

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

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

是否可以使用javascript测试用户浏览器对此功能的支持?

类似于CSS.supports()功能?

javascript regex
1个回答
0
投票

创建这样的测试并不难:

function supportsLookbehind(input, expected) {
    try {
        return input.replace(/(?<!\\)'/gi, '"') === expected;
    } catch (err) {
        //handle error
    }
    return false; //If it reaches here, it's not working as expected
}
© www.soinside.com 2019 - 2024. All rights reserved.