slevithan / regex

Regex template tag for readable, high-performance, native JS regexes, with context-aware interpolation and always-on best practices
MIT License
470 stars 9 forks source link

Is there a way to check if this package is supported? #2

Closed spaceemotion closed 3 months ago

spaceemotion commented 3 months ago

I stumbled over this package as I was trying to improve the performance of our RegExes and noticed the disclaimer about the v flag that's required. I know that not every customer has a "modern" machine, so is there a way to check if this package is supported at all?

Something like isSupported to check if I can even use this setup would be nice, so that I can implement a fall back for older browsers/systems.

I guess one way of doing it is to check via a try-catch, but I am not sure if I am missing anything else:

function isSupported() {
  try {
    new RegExp('', 'v');
    return true;
  } catch (e) {
    return false;
  }
}
slevithan commented 3 months ago

Your isSupported function should work great. Probably better is directly running regex``; behind your try catch, rather than new RegExp('', 'v');. You could also check for the existence of RegExp.prototype.unicodeSets so you don't need try/catch.

slevithan commented 3 months ago

As of v2.1.0, regex no longer relies on native support for flag v and works in much older browsers (see details in the readme under "Compatibility"). Testing for support should no longer be needed.