JavaScript之正则表达式(转载)
2010-03-02 23:30
447 查看
Regular Expression are very important in professional JavaScript.
There is a wonderful
resource in Mozilla developer center.
Two ways to construct a regular expression
1. Literal expression
2. Constructor function
Possible Flags
1. g - global search.
2. i - case-insensitive search.
3. m - multi-line search.
Simple and Special characters
A regular expression is composed of simple characters (such as /abc123_/) and special
characters.
Some important special characters:
1. ^ - Matches beginning of input.
2. $ - Matches end of input.
3. * - Matches the preceding character 0 or more times.
4. + - Matches the preceding character 1 or more times.
5. ? - Matches the preceding character 0 or 1 time.
6. . - Matches any single character.
Sepcial character \
There are two meanings of the special charater.
1. Used before simple character, indicating that the next character is special.
e.g. b is a simple character, \b is used to indicate a word
boundary.
Some important special character with \:
1. \b - Matches a word boundary.
2. \d - Matches a digit character.
3. \s - Matches a white space character, including space,
tab, line feed.
4. \w - Matches any alphanumeric character including the
underscore. Equivalent to [A-Za-z0-9_].
JavaScript funtions to operate regular expression
1. exec - returns: array.
2. match - returns: array or null.
3. test - returns: true or false.
4. search - returns: index or -1.
5. replace - returns: string.
Examples:
There is a wonderful
resource in Mozilla developer center.
Two ways to construct a regular expression
1. Literal expression
re = /ab+c/g;
2. Constructor function
re = new RegExp("ab+c", "g");
Possible Flags
1. g - global search.
2. i - case-insensitive search.
3. m - multi-line search.
Simple and Special characters
A regular expression is composed of simple characters (such as /abc123_/) and special
characters.
Some important special characters:
1. ^ - Matches beginning of input.
2. $ - Matches end of input.
3. * - Matches the preceding character 0 or more times.
4. + - Matches the preceding character 1 or more times.
5. ? - Matches the preceding character 0 or 1 time.
6. . - Matches any single character.
Sepcial character \
There are two meanings of the special charater.
1. Used before simple character, indicating that the next character is special.
e.g. b is a simple character, \b is used to indicate a word
boundary.
Some important special character with \:
1. \b - Matches a word boundary.
2. \d - Matches a digit character.
3. \s - Matches a white space character, including space,
tab, line feed.
4. \w - Matches any alphanumeric character including the
underscore. Equivalent to [A-Za-z0-9_].
JavaScript funtions to operate regular expression
1. exec - returns: array.
2. match - returns: array or null.
3. test - returns: true or false.
4. search - returns: index or -1.
5. replace - returns: string.
Examples:
/d(b+)d/g.exec("cdbbdbsbz"); // ["dbbd", "bb"] "cdbbdbsbz".match(/d(b+)d/g); // ["dbbd"] "cdbbdbsbz".search(/d(b+)d/g); // 1 /d(b+)d/g.test("cdbbdbsbz"); // true转自:/article/4676591.html
相关文章推荐
- [转载]javascript+ajax+jquery教程11--正则表达式
- javascript正则表达式(转载)
- <转载>javascript正则表达式
- 常用的JavaScript验证正则表达式(转载)
- [转载]温故知新 javascript 正则表达式
- [转载]JavaScript - 正则表达式
- JavaScript 1.10 RegExp类型—正则表达式的使用方法(部分转载)
- Javascript经典正则表达式(转载)
- 【转载】JavaScript正则表达式
- 温故知新 javascript 正则表达式(转载)
- Javascript中正则表达式详解(转载)
- joy--经典正则表达式 Javascript(转载)
- JavaScript内核系列 第6章 正则表达式(转载)
- 常用的JavaScript验证正则表达式(转载)
- 转载——javascript中的正则表达式及使用方法
- 常用的JavaScript验证正则表达式(转载)
- javascript 正则表达式
- JavaScript高级程序设计(第3版)学习笔记12——正则表达式
- JavaScript分步实现一个出生日期的正则表达式
- 调试JavaScript中正则表达式中遇到的问题