function prepareFlags(pattern, flags) {
    // Recent browsers throw on duplicate flags, so copy this behavior for nonnative flags
    if (clipDuplicates(flags) !== flags) {
        throw new SyntaxError(`Invalid duplicate regex flag ${flags}`);
    }

    // Strip and apply a leading mode modifier with any combination of flags except `dgy`
    pattern = pattern.replace(/^\(\?([\w$]+)\)/, ($0, $1) => {
        if (/[dgy]/.test($1)) {
            throw new SyntaxError(`Cannot use flags dgy in mode modifier ${$0}`);
        }
        // Allow duplicate flags within the mode modifier
        flags = clipDuplicates(flags + $1);
        return '';
    });

    // Throw on unknown native or nonnative flags
    for (const flag of flags) {
        if (!registeredFlags[flag]) {
            throw new SyntaxError(`Unknown regex flag ${flag}`);
        }
    }

    return {
        pattern,
        flags
    };
}
Checks for flag-related errors, and strips/applies flags in a leading mode modifier. Offloads
the flag preparation logic from the `XRegExp` constructor.

@private
@param {String} pattern Regex pattern, possibly with a leading mode modifier.
@param {String} flags Any combination of flags.
@returns {!Object} Object with properties `pattern` and `flags`.