function copy(regex, addFlags, removeFlags) {
        if (!self.isRegExp(regex)) {
            throw new TypeError("type RegExp expected");
        }
        var flags = nativ.replace.call(getNativeFlags(regex) + (addFlags || ""), duplicateFlags, "");
        if (removeFlags) {
            // Would need to escape `removeFlags` if this was public
            flags = nativ.replace.call(flags, new RegExp("[" + removeFlags + "]+", "g"), "");
        }
        if (regex.xregexp && !regex.xregexp.isNative) {
            // Compiling the current (rather than precompilation) source preserves the effects of nonnative source flags
            regex = augment(self(regex.source, flags),
                            regex.xregexp.captureNames ? regex.xregexp.captureNames.slice(0) : null);
        } else {
            // Augment with `XRegExp.prototype` methods, but use native `RegExp` (avoid searching for special tokens)
            regex = augment(new RegExp(regex.source, flags), null, true);
        }
        return regex;
    }
Copies a regex object while preserving special properties for named capture and augmenting with
`XRegExp.prototype` methods. The copy has a fresh `lastIndex` property (set to zero). Allows
adding and removing flags while copying the regex.
@private
@param {RegExp} regex Regex to copy.
@param {String} [addFlags] Flags to be added while copying the regex.
@param {String} [removeFlags] Flags to be removed while copying the regex.
@returns {RegExp} Copy of the provided regex, possibly with modified flags.