function copyRegex(regex, options) {
    if (!XRegExp.isRegExp(regex)) {
        throw new TypeError('Type RegExp expected');
    }

    var xData = regex[REGEX_DATA] || {};
    var flags = getNativeFlags(regex);
    var flagsToAdd = '';
    var flagsToRemove = '';
    var xregexpSource = null;
    var xregexpFlags = null;

    options = options || {};

    if (options.removeG) {flagsToRemove += 'g';}
    if (options.removeY) {flagsToRemove += 'y';}
    if (flagsToRemove) {
        flags = nativ.replace.call(flags, new RegExp('[' + flagsToRemove + ']+', 'g'), '');
    }

    if (options.addG) {flagsToAdd += 'g';}
    if (options.addY) {flagsToAdd += 'y';}
    if (flagsToAdd) {
        flags = clipDuplicates(flags + flagsToAdd);
    }

    if (!options.isInternalOnly) {
        if (xData.source !== undefined) {
            xregexpSource = xData.source;
        }
        // null or undefined; don't want to add to `flags` if the previous value was null, since
        // that indicates we're not tracking original precompilation flags
        if (xData.flags != null) {
            // Flags are only added for non-internal regexes by `XRegExp.globalize`. Flags are never
            // removed for non-internal regexes, so don't need to handle it
            xregexpFlags = flagsToAdd ? clipDuplicates(xData.flags + flagsToAdd) : xData.flags;
        }
    }

    // Augment with `XRegExp.prototype` properties, but use the native `RegExp` constructor to avoid
    // searching for special tokens. That would be wrong for regexes constructed by `RegExp`, and
    // unnecessary for regexes constructed by `XRegExp` because the regex has already undergone the
    // translation to native regex syntax
    regex = augment(
        new RegExp(options.source || regex.source, flags),
        hasNamedCapture(regex) ? xData.captureNames.slice(0) : null,
        xregexpSource,
        xregexpFlags,
        options.isInternalOnly
    );

    return regex;
}
Copies a regex object while preserving extended data and augmenting with `XRegExp.prototype`
properties. The copy has a fresh `lastIndex` property (set to zero). Allows adding and removing
flags g and y while copying the regex.

@private
@param {RegExp} regex Regex to copy.
@param {Object} [options] Options object with optional properties:
  - `addG` {Boolean} Add flag g while copying the regex.
  - `addY` {Boolean} Add flag y while copying the regex.
  - `removeG` {Boolean} Remove flag g while copying the regex.
  - `removeY` {Boolean} Remove flag y while copying the regex.
  - `isInternalOnly` {Boolean} Whether the copied regex will be used only for internal
    operations, and never exposed to users. For internal-only regexes, we can improve perf by
    skipping some operations like attaching `XRegExp.prototype` properties.
  - `source` {String} Overrides `<regex>.source`, for special cases.
@returns {RegExp} Copy of the provided regex, possibly with modified flags.