forked from andrewplummer/Sugar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regexp.js
105 lines (95 loc) · 2.28 KB
/
regexp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
'use strict';
/***
* @module RegExp
* @description RegExp escaping and flag manipulation.
*
* 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.
*
***/
defineStatic(sugarRegExp, {
/***
* @method escape([str] = '')
* @returns String
* @static
* @short Escapes all RegExp tokens in a string.
*
* @example
*
* RegExp.escape('really?') -> 'really\?'
* RegExp.escape('yes.') -> 'yes\.'
* RegExp.escape('(not really)') -> '\(not really\)'
*
* @param {string} str
*
***/
'escape': function(str) {
return escapeRegExp(str);
}
});
defineInstance(sugarRegExp, {
/***
* @method getFlags()
* @returns String
* @short Returns the flags of the regex as a string.
*
* @example
*
* /texty/gim.getFlags() -> 'gim'
*
***/
'getFlags': function(r) {
return getRegExpFlags(r);
},
/***
* @method setFlags(flags)
* @returns RegExp
* @short Creates a copy of the regex with `flags` set.
*
* @example
*
* /texty/.setFlags('gim') -> now has global, ignoreCase, and multiline set
*
* @param {string} flags
*
***/
'setFlags': function(r, flags) {
return RegExp(r.source, flags);
},
/***
* @method addFlags(flags)
* @returns RegExp
* @short Creates a copy of the regex with `flags` added.
*
* @example
*
* /texty/.addFlags('g') -> /texty/g
* /texty/.addFlags('im') -> /texty/im
*
* @param {string} flags
*
***/
'addFlags': function(r, flags) {
return RegExp(r.source, getRegExpFlags(r, flags));
},
/***
* @method removeFlags(flags)
* @returns RegExp
* @short Creates a copy of the regex with `flags` removed.
*
* @example
*
* /texty/gim.removeFlags('g') -> /texty/im
* /texty/gim.removeFlags('im') -> /texty/g
*
* @param {string} flags
*
***/
'removeFlags': function(r, flags) {
var reg = allCharsReg(flags);
return RegExp(r.source, getRegExpFlags(r).replace(reg, ''));
}
});