master
1
2 'use strict';
3
4 /***
5 * @package RegExp
6 * @dependency core
7 * @description Escaping regexes and manipulating their flags.
8 *
9 * Note here that methods on the RegExp class like .exec and .test will fail in the current version of SpiderMonkey being
10 * used by CouchDB when using shorthand regex notation like /foo/. This is the reason for the intermixed use of shorthand
11 * and compiled regexes here. If you're using JS in CouchDB, it is safer to ALWAYS compile your regexes from a string.
12 *
13 ***/
14
15 extend(regexp, false, true, {
16
17 /***
18 * @method RegExp.escape(<str> = '')
19 * @returns String
20 * @short Escapes all RegExp tokens in a string.
21 * @example
22 *
23 * RegExp.escape('really?') -> 'really\?'
24 * RegExp.escape('yes.') -> 'yes\.'
25 * RegExp.escape('(not really)') -> '\(not really\)'
26 *
27 ***/
28 'escape': function(str) {
29 return escapeRegExp(str);
30 }
31
32 });
33
34 extend(regexp, true, true, {
35
36 /***
37 * @method getFlags()
38 * @returns String
39 * @short Returns the flags of the regex as a string.
40 * @example
41 *
42 * /texty/gim.getFlags('testy') -> 'gim'
43 *
44 ***/
45 'getFlags': function() {
46 return getRegExpFlags(this);
47 },
48
49 /***
50 * @method setFlags(<flags>)
51 * @returns RegExp
52 * @short Sets the flags on a regex and retuns a copy.
53 * @example
54 *
55 * /texty/.setFlags('gim') -> now has global, ignoreCase, and multiline set
56 *
57 ***/
58 'setFlags': function(flags) {
59 return regexp(this.source, flags);
60 },
61
62 /***
63 * @method addFlag(<flag>)
64 * @returns RegExp
65 * @short Adds <flag> to the regex.
66 * @example
67 *
68 * /texty/.addFlag('g') -> now has global flag set
69 *
70 ***/
71 'addFlag': function(flag) {
72 return this.setFlags(getRegExpFlags(this, flag));
73 },
74
75 /***
76 * @method removeFlag(<flag>)
77 * @returns RegExp
78 * @short Removes <flag> from the regex.
79 * @example
80 *
81 * /texty/g.removeFlag('g') -> now has global flag removed
82 *
83 ***/
84 'removeFlag': function(flag) {
85 return this.setFlags(getRegExpFlags(this).replace(flag, ''));
86 }
87
88 });
89
90