Testing for a feature of regex compilation in IE11

前端 未结 2 521
陌清茗
陌清茗 2021-01-24 19:26

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

相关标签:
2条回答
  • 2021-01-24 19:57

    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);
    
    0 讨论(0)
  • 2021-01-24 20:02

    If you feel comfortable with something like Function or eval, and a try/catch...something like this should work:

    try { 
      return new Function('', 'return /\\p{Script=Greek}/u;')() 
    } catch(e) { return /asdf/g;}
    

    Note you have to escape the \\p because it's contained in a string now. Tested this on IE11 and it worked (fell back without a fatal parsing error)

    0 讨论(0)
提交回复
热议问题