4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='RegExp'>/**
19 </span> * @class RegExp
21 * Creates a regular expression object for matching text according to a pattern.
23 * When using the constructor function, the normal string escape rules (preceding
24 * special characters with \ when included in a string) are necessary. For
25 * example, the following are equivalent:
27 * var re = new RegExp("\\w+");
30 * Notice that the parameters to the literal format do not use quotation marks to
31 * indicate strings, while the parameters to the constructor function do use
32 * quotation marks. So the following expressions create the same regular
36 * new RegExp("ab+c", "i");
38 * # Special characters in regular expressions
40 * | Character | Meaning
41 * |:-----------------|:--------------------------------------------------------------------------------------
42 * | `\` | For characters that are usually treated literally, indicates that the next character
43 * | | is special and not to be interpreted literally.
44 * | | For example, `/b/` matches the character 'b'. By placing a backslash in front of b, that
45 * | | is by using `/\b/`, the character becomes special to mean match a word boundary.
49 * | | For characters that are usually treated specially, indicates that the next character is
50 * | | not special and should be interpreted literally.
52 * | | For example, `*` is a special character that means 0 or more occurrences of the preceding
53 * | | character should be matched; for example, `/a*\/` means match 0 or more "a"s. To match *
54 * | | literally, precede it with a backslash; for example, `/a\*\/` matches 'a*'.
56 * | `^` | Matches beginning of input. If the multiline flag is set to true, also matches
57 * | | immediately after a line break character.
59 * | | For example, `/^A/` does not match the 'A' in "an A", but does match the first 'A' in
60 * | | "An A".
62 * | `$` | Matches end of input. If the multiline flag is set to true, also matches immediately
63 * | | before a line break character.
65 * | | For example, `/t$/` does not match the 't' in "eater", but does match it in "eat".
67 * | `*` | Matches the preceding item 0 or more times.
69 * | | For example, `/bo*\/` matches 'boooo' in "A ghost booooed" and 'b' in "A bird warbled",
70 * | | but nothing in "A goat grunted".
72 * | `+` | Matches the preceding item 1 or more times. Equivalent to `{1,}`.
74 * | | For example, `/a+/` matches the 'a' in "candy" and all the a's in "caaaaaaandy".
76 * | `?` | Matches the preceding item 0 or 1 time.
78 * | | For example, `/e?le?/` matches the 'el' in "angel" and the 'le' in "angle."
80 * | | If used immediately after any of the quantifiers `*`, `+`, `?`, or `{}`, makes the quantifier
81 * | | non-greedy (matching the minimum number of times), as opposed to the default, which is
82 * | | greedy (matching the maximum number of times).
84 * | | Also used in lookahead assertions, described under `(?=)`, `(?!)`, and `(?:)` in this table.
86 * | `.` | (The decimal point) matches any single character except the newline characters: \n \r
87 * | | \u2028 or \u2029. (`[\s\S]` can be used to match any character including new lines.)
89 * | | For example, `/.n/` matches 'an' and 'on' in "nay, an apple is on the tree", but not 'nay'.
91 * | `(x)` | Matches `x` and remembers the match. These are called capturing parentheses.
93 * | | For example, `/(foo)/` matches and remembers 'foo' in "foo bar." The matched substring can
94 * | | be recalled from the resulting array's elements `[1], ..., [n]` or from the predefined RegExp
95 * | | object's properties `$1, ..., $9`.
97 * | `(?:x)` | Matches `x` but does not remember the match. These are called non-capturing parentheses.
98 * | | The matched substring can not be recalled from the resulting array's elements `[1], ..., [n]`
99 * | | or from the predefined RegExp object's properties `$1, ..., $9`.
101 * | `x(?=y)` | Matches `x` only if `x` is followed by `y`. For example, `/Jack(?=Sprat)/` matches 'Jack' only if
102 * | | it is followed by 'Sprat'. `/Jack(?=Sprat|Frost)/` matches 'Jack' only if it is followed by
103 * | | 'Sprat' or 'Frost'. However, neither 'Sprat' nor 'Frost' is part of the match results.
105 * | `x(?!y)` | Matches `x` only if `x` is not followed by `y`. For example, `/\d+(?!\.)/` matches a number only
106 * | | if it is not followed by a decimal point.
108 * | | `/\d+(?!\.)/.exec("3.141")` matches 141 but not 3.141.
110 * | `x|y` | Matches either `x` or `y`.
112 * | | For example, `/green|red/` matches 'green' in "green apple" and 'red' in "red apple."
114 * | `{n}` | Where `n` is a positive integer. Matches exactly n occurrences of the preceding item.
116 * | | For example, `/a{2}/` doesn't match the 'a' in "candy," but it matches all of the a's
117 * | | in "caandy," and the first two a's in "caaandy."
119 * | `{n,}` | Where `n` is a positive integer. Matches at least n occurrences of the preceding item.
121 * | | For example, `/a{2,}/` doesn't match the 'a' in "candy", but matches all of the a's in
122 * | | "caandy" and in "caaaaaaandy."
124 * | `{n,m}` | Where `n` and `m` are positive integers. Matches at least `n` and at most `m` occurrences of the
125 * | | preceding item.
127 * | | For example, `/a{1,3}/` matches nothing in "cndy", the 'a' in "candy," the first two a's
128 * | | in "caandy," and the first three a's in "caaaaaaandy". Notice that when matching
129 * | | "caaaaaaandy", the match is "aaa", even though the original string had more a's in it.
131 * | `[xyz]` | A character set. Matches any one of the enclosed characters. You can specify a range of
132 * | | characters by using a hyphen.
134 * | | For example, `[abcd]` is the same as `[a-d]`. They match the 'b' in "brisket" and the 'c'
135 * | | in "chop".
137 * | `[^xyz]` | A negated or complemented character set. That is, it matches anything that is not
138 * | | enclosed in the brackets. You can specify a range of characters by using a hyphen.
140 * | | For example, `[^abc]` is the same as `[^a-c]`. They initially match 'r' in "brisket" and
141 * | | 'h' in "chop."
143 * | `[\b]` | Matches a backspace. (Not to be confused with `\b`.)
145 * | `\b` | Matches a word boundary, such as a space. (Not to be confused with `[\b]`.)
147 * | | For example, `/\bn\w/` matches the 'no' in "noonday"; `/\wy\b/` matches the 'ly' in
148 * | | "possibly yesterday."
150 * | `\B` | Matches a non-word boundary.
152 * | | For example, `/\w\Bn/` matches 'on' in "noonday", and `/y\B\w/` matches 'ye' in "possibly
153 * | | yesterday."
155 * | `\cX` | Where X is a letter from A - Z. Matches a control character in a string.
157 * | | For example, `/\cM/` matches control-M in a string.
159 * | `\d` | Matches a digit character in the basic Latin alphabet. Equivalent to `[0-9]`.
161 * | | For example, `/\d/` or `/[0-9]/` matches '2' in "B2 is the suite number."
163 * | `\D` | Matches any non-digit character in the basic Latin alphabet. Equivalent to `[^0-9]`.
165 * | | For example, `/\D/` or `/[^0-9]/` matches 'B' in "B2 is the suite number.
167 * | `\f` | Matches a form-feed.
169 * | `\n` | Matches a linefeed.
171 * | `\r` | Matches a carriage return.
173 * | `\s` | Matches a single white space character, including space, tab, form feed, line feed and
174 * | | other unicode spaces. Equivalent to:
176 * | | `[\t\n\v\f\r \u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000]`
178 * | | For example, `/\s\w*\/` matches ' bar' in "foo bar."
180 * | `\S` | Matches a single character other than white space. Equivalent to:
182 * | | `[^\t\n\v\f\r \u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000]`
184 * | | For example, `/\S\w*\/` matches 'foo' in "foo bar."
186 * | `\t` | Matches a tab.
188 * | `\v` | Matches a vertical tab.
190 * | `\w` | Matches any alphanumeric character from the basic Latin alphabet, including the
191 * | | underscore. Equivalent to `[A-Za-z0-9_]`.
193 * | | For example, `/\w/` matches 'a' in "apple," '5' in "$5.28," and '3' in "3D."
195 * | `\W` | Matches any character that is not a word character from the basic Latin alphabet. Equivalent
196 * | | to `[^A-Za-z0-9_]`.
198 * | | For example, `/\W/` or `/[^A-Za-z0-9_]/` matches '%' in "50%."
200 * | `\n` | Where `n` is a positive integer. A back reference to the last substring matching the n
201 * | | parenthetical in the regular expression (counting left parentheses).
203 * | | For example, `/apple(,)\sorange\1/` matches 'apple, orange,' in "apple, orange, cherry,
204 * | | peach." A more complete example follows this table.
206 * | `\0` | Matches a NULL character. Do not follow this with another digit.
208 * | `\xhh` | Matches the character with the code `hh` (two hexadecimal digits)
210 * | `\uhhhh` | Matches the character with the Unicode value `hhhh` (four hexadecimal digits)
212 * The literal notation provides compilation of the regular expression when the expression is evaluated. Use
213 * literal notation when the regular expression will remain constant. For example, if you use literal notation
214 * to construct a regular expression used in a loop, the regular expression won't be recompiled on each iteration.
216 * The constructor of the regular expression object, for example, new RegExp("ab+c"), provides runtime
217 * compilation of the regular expression. Use the constructor function when you know the regular expression
218 * pattern will be changing, or you don't know the pattern and are getting it from another source, such as user input.
220 * <div class="notice">
221 * Documentation for this class comes from <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp">MDN</a>
222 * and is available under <a href="http://creativecommons.org/licenses/by-sa/2.0/">Creative Commons: Attribution-Sharealike license</a>.
226 <span id='RegExp-method-constructor'>/**
227 </span> * @method constructor
228 * Creates new regular expression object.
230 * @param {String} pattern
231 * The text of the regular expression.
232 * @param {String} flags
233 * If specified, flags can have any combination of the following values:
235 * - "g" - global match
236 * - "i" - ignore case
237 * - "m" - Treat beginning and end characters (^ and $) as working over multiple lines
238 * (i.e., match the beginning or end of _each_ line (delimited by \n or \r), not
239 * only the very beginning or end of the whole input string)
244 <span id='RegExp-method-exec'>/**
245 </span> * @method exec
246 * Executes a search for a match in its string parameter.
248 * If the match succeeds, the `exec` method returns an array and updates properties of the regular
249 * expression object. The returned array has the matched text as the first item, and then one item for
250 * each capturing parenthesis that matched containing the text that was captured. If the match fails,
251 * the `exec` method returns `null`.
253 * If you are executing a match simply to find true or false, use the `test` method or the `String
256 * Consider the following example:
258 * // Match one d followed by one or more b's followed by one d
259 * // Remember matched b's and the following d
261 * var re = /d(b+)(d)/ig;
262 * var result = re.exec("cdbBdbsbz");
264 * The following table shows the results for this script:
266 * | Object | Property/Index | Description | Example
267 * |:-----------------|:---------------|:---------------------------------------------------------------------|:---------------------
268 * | `result` | | The content of myArray. | `["dbBd", "bB", "d"]`
269 * | | `index` | The 0-based index of the match in the string | `1`
270 * | | `input` | The original string. | `cdbDdbsbz`
271 * | | `[0]` | The last matched characters. | `dbBd`
272 * | | `[1], ...[n]` | The parenthesized substring matches, if any. The number of possible | `[1] = bB`
273 * | | | parenthesized substrings is unlimited. | `[2] = d`
274 * | `re` | `lastIndex` | The index at which to start the next match. | `5`
275 * | | `ignoreCase` | Indicates the "`i`" flag was used to ignore case. | `true`
276 * | | `global` | Indicates the "`g`" flag was used for a global match. | `true`
277 * | | `multiline` | Indicates the "`m`" flag was used to search in strings across | `false`
278 * | | | multiple lines. |
279 * | | `source` | The text of the pattern. | d(b+)(d)
281 * If your regular expression uses the "`g`" flag, you can use the `exec` method multiple times to find
282 * successive matches in the same string. When you do so, the search starts at the substring of `str`
283 * specified by the regular expression's `lastIndex` property (`test` will also advance the `lastIndex`
284 * property). For example, assume you have this script:
286 * var myRe = /ab*\/g;
287 * var str = "abbcdefabh";
289 * while ((myArray = myRe.exec(str)) != null)
291 * var msg = "Found " + myArray[0] + ". ";
292 * msg += "Next match starts at " + myRe.lastIndex;
296 * This script displays the following text:
298 * Found abb. Next match starts at 3
299 * Found ab. Next match starts at 9
301 * You can also use `exec()` without creating a RegExp object:
303 * var matches = /(hello \S+)/.exec('This is a hello world!');
306 * This will display an alert containing 'hello world!';
308 * @param {String} str The string against which to match the regular expression.
309 * @return {Array} Array of results or `NULL`.
312 <span id='RegExp-method-test'>/**
313 </span> * @method test
314 * Tests for a match in its string parameter.
316 * When you want to know whether a pattern is found in a string use the test method (similar to the
317 * `String.search` method); for more information (but slower execution) use the exec method (similar to
318 * the `String.match` method). As with exec (or in combination with it), test called multiple times on
319 * the same global regular expression instance will advance past the previous match.
321 * The following example prints a message which depends on the success of the test:
323 * function testinput(re, str){
325 * midstring = " contains ";
327 * midstring = " does not contain ";
328 * document.write (str + midstring + re.source);
331 * @param {String} str The string against which to match the regular expression.
332 * @return {Boolean} true if string contains any matches, otherwise returns false.
335 <span id='RegExp-method-toString'>/**
336 </span> * @method toString
337 * Returns a string representing the specified object. Overrides the `Object.prototype.toString`
340 * The RegExp object overrides the `toString` method of the `Object` object; it does not inherit
341 * `Object.toString`. For RegExp objects, the `toString` method returns a string representation of the
342 * regular expression.
344 * The following example displays the string value of a RegExp object:
346 * myExp = new RegExp("a+b+c");
347 * alert(myExp.toString()); // displays "/a+b+c/"
349 * @return {String} Regular expression as a string.
354 // Note that several of the RegExp properties have both long and short (Perl-like) names.
355 // Both names always refer to the same value. Perl is the programming language from which
356 // JavaScript modeled its regular expressions.
358 <span id='RegExp-property-global'>/**
359 </span> * @property {Boolean} global
360 * Whether to test the regular expression against all possible matches in a
361 * string, or only against the first.
363 * `global` is a property of an individual regular expression object.
365 * The value of `global` is true if the "`g`" flag was used; otherwise, `false`. The "`g`" flag
366 * indicates that the regular expression should be tested against all possible matches in a string.
368 * You cannot change this property directly.
371 <span id='RegExp-property-ignoreCase'>/**
372 </span> * @property {Boolean} ignoreCase
373 * Whether to ignore case while attempting a match in a string.
375 * `ignoreCase` is a property of an individual regular expression object.
377 * The value of `ignoreCase` is true if the "`i`" flag was used; otherwise, false. The "`i`" flag indicates
378 * that case should be ignored while attempting a match in a string.
380 * You cannot change this property directly.
383 <span id='RegExp-property-lastIndex'>/**
384 </span> * @property {Number} lastIndex
385 * The index at which to start the next match. A read/write integer property that specifies the index
386 * at which to start the next match.
388 * `lastIndex` is a property of an individual regular expression object.
390 * This property is set only if the regular expression used the "`g`" flag to indicate a global search.
391 * The following rules apply:
393 * - If `lastIndex` is greater than the length of the string, `regexp.test` and `regexp.exec` fail,
394 * and `lastIndex` is set to 0.
395 * - If `lastIndex` is equal to the length of the string and if the regular expression matches the
396 * empty string, then the regular expression matches input starting at `lastIndex`.
397 * - If `lastIndex` is equal to the length of the string and if the regular expression does not match
398 * the empty string, then the regular expression mismatches input, and `lastIndex` is reset to 0.
399 * - Otherwise, `lastIndex` is set to the next position following the most recent match.
401 * For example, consider the following sequence of statements:
403 * - `re = /(hi)?/g` Matches the empty string.
404 * - `re("hi")` Returns `["hi", "hi"]` with `lastIndex` equal to 2.
405 * - `re("hi")` Returns `[""]`, an empty array whose zeroth element is the match string. In this
406 * case, the empty string because `lastIndex` was 2 (and still is 2) and "`hi`" has length 2.
409 <span id='RegExp-property-multiline'>/**
410 </span> * @property {Boolean} multiline
411 * Whether or not to search in strings across multiple lines.
413 * `multiline` is a property of an individual regular expression object..
415 * The value of `multiline` is true if the "`m`" flag was used; otherwise, `false`. The "`m`" flag
416 * indicates that a multiline input string should be treated as multiple lines. For example, if "`m`"
417 * is used, "`^`" and "`$`" change from matching at only the start or end of the entire string to the
418 * start or end of any line within the string.
420 * You cannot change this property directly.
423 <span id='RegExp-property-source'>/**
424 </span> * @property {String} source
425 * The text of the pattern.
427 * A read-only property that contains the text of the pattern, excluding the forward slashes.
429 * `source` is a property of an individual regular expression object.
431 * You cannot change this property directly.