Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to modify flags on an existing RegExp?

Tags:

My code receives a RegExp object (out of my control). It isn't global but I need it to be.

At the moment I'm doing this:

if (!regex.global) {
  var flags = 'g';
  if (regex.ignoreCase) flags += 'i';
  if (regex.multiline ) flags += 'm';
  if (regex.sticky    ) flags += 'y';
  regex = new RegExp(regex.source, flags);
}

...because I can't figure out any other way.

  • regex.global doesn't have a setter.
  • regex.compile(new_pattern) is deprecated in favour of new RegExp(new_pattern)
  • regex.flags isn't a thing

Is there a better way to do this?