在 IE11 中测试正则表达式编译的一个特性 [英] Testing for a feature of regex compilation in IE11

查看:52
本文介绍了在 IE11 中测试正则表达式编译的一个特性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试 Unicode Regex Property Escapes 以避免在浏览器中编译损坏的正则表达式不支持它(例如 IE11).理想情况下,我想写一些类似的东西:

I would like to test for Unicode Regex Property Escapes to avoid compiling a broken regular expression in browsers that do not support it (for example IE11). Ideally, I'd like to write something along the lines of:

if (regex supports unicode property escapes) {
    return /\p{Emoji_Modifier_Base}\p{Emoji_Modifier}?|\p{Emoji_Presentation}|\p{Emoji}\uFE0F/gu;
} else {
    return /somereplacementregex/;
}

这可能吗?我知道有一些功能检测 API,但据我所知,没有一个用于正则表达式.或者,如果有另一种编写方式不会破坏编译并且会抛出我可以捕获的错误,那么这也很棒.

Is this possible at all? I know there are some feature detection API's but as far as I know there isn't one for regex. Alternatively if there's another way of writing this that won't break the compilation and would throw an error I can catch then that would also be great.

推荐答案

这适用于 Firefox 或 Safari;在 IE11 中应该没问题:

This works in Firefox or Safari; should be okay in IE11:

let regex = /somereplacementregex/;
try
{
    regex = new RegExp ("\\p{Emoji_Modifier_Base}\\p{Emoji_Modifier}?|\\p{Emoji_Presentation}|\\p{Emoji}\\uFE0F", "gu");
}
catch (e) { }
//
// use regex...
console.log (regex);

这篇关于在 IE11 中测试正则表达式编译的一个特性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆