JavaScript正则

正则匹配
Alphanumeric characterItself
\0The NUL character(\u000)
\tTab(\u009)
\nNewline
\vVertical tab
\fForm feed
\rCarriage return
\xnnThe Latin character
\uxxxxThe Unicode character
\cXThe 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
\wAny ASCII word character [a-zA-Z0-9]
\WAny character that is not an ASCII word character
\sAny Unicode whitespace character
\SAny character that is not Unicode whitespace
\d

Any ASCII digit. Equivalent to [0-9].

\DAny 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);

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值