Alphanumeric character | Itself |
\0 | The NUL character(\u000) |
\t | Tab(\u009) |
\n | Newline |
\v | Vertical tab |
\f | Form feed |
\r | Carriage return |
\xnn | The Latin character |
\uxxxx | The Unicode character |
\cX | The control character |
[...] | Any one character between the brackets |
[^...] | Any one character not between the brackets |
. | Any character except newline or another unicode new line terminator |
\w | Any ASCII word character [a-zA-Z0-9] |
\W | Any character that is not an ASCII word character |
\s | Any Unicode whitespace character |
\S | Any character that is not Unicode whitespace |
\d | Any ASCII digit. Equivalent to [0-9]. |
\D | Any character other than an ASCII digit |
[\b] | A literal backspace (special case) |
{n,m} | Match the previous item at least n times but no more thanm times. |
{n,} | Match the previous item n or more times. |
{n} | Match exactly n occurrences of the previous item. |
? | Match zero or one occurrences of the previous item. That is, the previous item is optional. Equivalent to{0,1}. |
+ | Match one or more occurrences of the previous item. Equivalent to{1,}. |
* | Match zero or more occurrences of the previous item. Equivalent to{0,}. |
1. search replace match split
Strings support fourmethods that make use of regular expressions. The simplest is search( ). Thismethod takes a regular expression argument and returns either the characterposition of the start of the first matching substring, or -1 if there is no match.For example, the following call returns 4:
"JavaScript".search(/script/i);
var quote =/"([^"]*)"/g;
// Replace thestraight quotation marks with "curly quotes,"
// and leave thecontents of the quote (stored in $1) unchanged.
text.replace(quote,"``$1''");
"1 plus 2 equals3".match(/\d+/g) // returns["1", "2", "3"]
"123,456,789".split(","); // Returns["123","456","789"]
The split( ) methodcan also take a regular expression as its argument. This ability makes themethod more powerful. For example, we can now specify a separator characterthat allows an arbitrary amount of whitespace on either side:
"1,2, 3 , 4,5".split(/\s*,\s*/); // Returns["1","2","3","4","5"]
exec会记录最后搜索的位置,test大部分和exec一样,只是返回值有区别?。
Calling test( ) isequivalent to calling exec( ) and returning true if the return value of exec( )is not null.
说明:根据手册,exec只返回匹配结果的第一个值,比如上例如果不用while循环,将只返回'I'(尽管i空格后的love和you都符合表达式),无论re表达式用不用全局标记g。但是如果为正则表达式设置了全局标记g,exec从以 lastIndex 的值指示的位置开始查找。如果没有设置全局标志,exec 忽略 lastIndex的值,从字符串的起始位置开始搜索。利用这个特点可以反复调用exec遍历所有匹配,等价于match具有g标志。
test 方法
返回一个 Boolean值,它指出在被查找的字符串中是否匹配给出的正则表达式。
Unlike the match( )method, exec( ) returns the same kind of array whether or not the regularexpression has the global g flag. Recall that match( ) returns an array ofmatches when passed a global regular expression. exec( ), by contrast, alwaysreturns a single match and provides complete information about that match. Whenexec( ) is called for a regular expression that has the g flag, it sets thelastIndex property of the regular expression object to the character positionimmediately following the matched substring. When exec( ) is invoked a secondtime for the same regular expression, it begins its search at the characterposition indicated by the lastIndex property. If exec( ) does not find a match,it resets lastIndex to 0. (You can also set lastIndex to 0 at any time, whichyou should do whenever you quit a search before you find the last match in onestring and begin searching another string with the same RegExp object.) Thisspecial behavior allows us to call exec( ) repeatedly in order to loop throughall the regular expression matches in a string. For example:
var pattern =/Java/g;
var text ="JavaScript is more fun than Java!";
var result;
while((result =pattern.exec(text)) != null) {
alert("Matched `" + result[0] +"'" +
" at position " +result.index +
"; next search begins at " +pattern.lastIndex);
}