Core JavaScript 1.5 Reference:Global Objects:RegExp
From MDC
目录 |
[编辑] 摘要
核心对象
一个正则表达式对象包含了匹配模式。包含了通过使用模式来查找替换匹配字符串的属性方法。
A regular expression object contains the pattern of a regular expression. It has properties and methods for using that regular expression to find and replace matches in strings.
另外,不通通常表达类的特性产生用RegExp结构函数,无论何时任意所用的正规表达,预定义RegExp类有统计特性设置。
In addition to the properties of an individual regular expression object that you create using the RegExp constructor function, the predefined RegExp object has static properties that are set whenever any regular expression is used.
[编辑] 创建
文本形式或者 RegExp 构造函数.
文本形式使用如下:
/pattern/flags
构造函数使用如下:
new RegExp("pattern"[, "flags"])
[编辑] 参数
-
pattern - 正则表达式文本.
-
flags - 如果指定, flags 可以是以下值的任何组合:
g- 全局匹配,i- 忽略大小写,m- 多行匹配.
注意文本形式的表达式没有引号, 而带参数的构造函数要有引号. 所以以下两种创建正则表达式对象都是相同的:
/ab+c/i
new RegExp("ab+c", "i")
[编辑] 描述
When using the constructor function, the normal string escape rules (preceding special characters with \ when included in a string) are necessary. For example, the following are equivalent:
re = new RegExp("\\w+")
re = /\w+/
[编辑] 正则表达式中的特殊字符
| Character | Meaning |
\ |
For characters that are usually treated literally, indicates that the next character is special and not to be interpreted literally. For example, -or- For characters that are usually treated specially, indicates that the next character is not special and should be interpreted literally. For example, * is a special character that means 0 or more occurrences of the preceding character should be matched; for example, |
^ |
Matches beginning of input. If the multiline flag is set to true, also matches immediately after a line break character. For example, |
$ |
Matches end of input. If the multiline flag is set to true, also matches immediately before a line break character. For example, |
* |
Matches the preceding item 0 or more times. For example, |
+ |
Matches the preceding item 1 or more times. Equivalent to For example, |
? |
Matches the preceding item 0 or 1 time. For example, If used immediately after any of the quantifiers Also used in lookahead assertions, described under |
. |
(The decimal point) matches any single character except the newline characters: \n \r \u2028 or \u2029. ( For example, |
(x) |
Matches For example, |
(?:x) |
Matches |
x(?=y) |
Matches |
x(?!y) |
Matches
|
x|y |
Matches either For example, |
{n} |
Where For example, |
{n,} |
Where For example, |
{n,m} |
Where For example, |
[xyz] |
A character set. Matches any one of the enclosed characters. You can specify a range of characters by using a hyphen. For example, |
[^xyz] |
A negated or complemented character set. That is, it matches anything that is not enclosed in the brackets. You can specify a range of characters by using a hyphen. For example, |
[\b] |
Matches a backspace. (Not to be confused with |
\b |
Matches a word boundary, such as a space. (Not to be confused with For example, |
\B |
Matches a non-word boundary. For example, |
\cX |
Where For example, |
\d |
Matches a digit character from any alphabet. Use For example, |
\D |
Matches any non-digit character (all alphabets). For example, |
\f |
Matches a form-feed. |
\n |
Matches a linefeed. |
\r |
Matches a carriage return. |
\s |
Matches a single white space character, including space, tab, form feed, line feed and other unicode spaces.[1] For example, |
\S |
Matches a single character other than white space.[2] For example, |
\t |
Matches a tab. |
\v |
Matches a vertical tab. |
\w |
Matches any (Basic Latin alphabet) alphanumeric character including the underscore. Equivalent to For example, |
\W |
Matches any non-(Basic Latin)word character. Equivalent to For example, |
\n |
Where For example, |
\0 |
Matches a NUL character. Do not follow this with another digit. |
\xhh |
Matches the character with the code |
\uhhhh |
Matches the character with code |
The literal notation provides compilation of the regular expression when the expression is evaluated. Use literal notation when the regular expression will remain constant. For example, if you use literal notation to construct a regular expression used in a loop, the regular expression won't be recompiled on each iteration.
The constructor of the regular expression object, for example, new RegExp("ab+c"), provides runtime compilation of the regular expression. Use the constructor function when you know the regular expression pattern will be changing, or you don't know the pattern and are getting it from another source, such as user input.
A separate predefined RegExp object is available in each window; that is, each separate thread of JavaScript execution gets its own RegExp object. Because each script runs to completion without interruption in a thread, this assures that different scripts do not overwrite values of the RegExp object.
- ^ Equivalent to:
[\t\n\v\f\r \u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000]
- ^ Equivalent to:
[^\t\n\v\f\r \u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000]
[编辑] 属性
Note that several of the RegExp properties have both long and short (Perl-like) names. Both names always refer to the same value. Perl is the programming language from which JavaScript modeled its regular expressions.
constructor: Specifies the function that creates an object's prototype.
global: Whether to test the regular expression against all possible matches in a string, or only against the first. As of JavaScript 1.5, a property of a RegExp instance, not the RegExp object.
ignoreCase: Whether to ignore case while attempting a match in a string. As of JavaScript 1.5, a property of a RegExp instance, not the RegExp object.
lastIndex: The index at which to start the next match. As of JavaScript 1.5, a property of a RegExp instance, not the RegExp object.
multiline: Whether or not to search in strings across multiple lines. As of JavaScript 1.5, a property of a RegExp instance, not the RegExp object.
prototype: Allows the addition of properties to all objects.
source: The text of the pattern. As of JavaScript 1.5, a property of a RegExp instance, not the RegExp object.
[编辑] 方法
exec: Executes a search for a match in its string parameter.
test: Tests for a match in its string parameter.
toSource: Returns an object literal representing the specified object; you can use this value to create a new object. Overrides the Object.toSource method.
toString: Returns a string representing the specified object. Overrides the Object.toString method.
In addition, this object inherits the watch and unwatch methods from Object.
[编辑] 示例
[编辑] Example: Using the replace method
The following script uses the replace method to switch the words in the string. In the replacement text, the script uses "$1" and "$2" to indicate the results of the corresponding matching parentheses in the regular expression pattern.
var re = /(\w+)\s(\w+)/; var str = "John Smith"; var newstr = str.replace(re, "$2, $1"); document.write(newstr);
This displays "Smith, John".
[编辑] See also
- The Core JavaScript 1.5 Guide:Regular Expressions chapter.