UNPKG

sugar

Version:

A Javascript library for working with native objects.

94 lines (79 loc) 2.32 kB
/*** * RegExp module * @dependency core * * Note here that methods on the RegExp class like .exec and .test will fail in the current version of SpiderMonkey being * used by CouchDB when using shorthand regex notation like /foo/. This is the reason for the intermixed use of shorthand * and compiled regexes here. If you're using JS in CouchDB, it is safer to ALWAYS compile your regexes from a string. * ***/ regexp.NPCGSupport = isUndefined(regexp('()??').exec('')[1]); // NPCG: nonparticipating capturing group function uniqueRegExpFlags(flags) { return flags.split('').sort().join('').replace(/([gimy])\1+/g, '$1'); } extend(regexp, false, false, { /*** * @method RegExp.escape(<str> = '') * @returns String * @short Escapes all RegExp tokens in a string. * @example * * RegExp.escape('really?') -> 'really\?' * RegExp.escape('yes.') -> 'yes\.' * RegExp.escape('(not really)') -> '\(not really\)' * ***/ 'escape': function(str) { return escapeRegExp(str); } }); extend(regexp, true, false, { /*** * @method getFlags() * @returns String * @short Returns the flags of the regex as a string. * @example * * /texty/gim.getFlags('testy') -> 'gim' * ***/ 'getFlags': function() { return getRegExpFlags(this); }, /*** * @method setFlags(<flags>) * @returns RegExp * @short Sets the flags on a regex and retuns a copy. * @example * * /texty/.setFlags('gim') -> now has global, ignoreCase, and multiline set * ***/ 'setFlags': function(flags) { return regexp(this.source, flags); }, /*** * @method addFlag(<flag>) * @returns RegExp * @short Adds <flag> to the regex. * @example * * /texty/.addFlag('g') -> now has global flag set * ***/ 'addFlag': function(flag) { return this.setFlags(getRegExpFlags(this, flag)); }, /*** * @method removeFlag(<flag>) * @returns RegExp * @short Removes <flag> from the regex. * @example * * /texty/g.removeFlag('g') -> now has global flag removed * ***/ 'removeFlag': function(flag) { return this.setFlags(getRegExpFlags(this).replace(flag, '')); } });