日期:2014-05-16 浏览次数:20369 次
var regExp = /^Sol[0-9]$/gi;
var isContained = regExp.test(value);
alert("regExp.test("+value+")="+regExp.test(value) + " isContained="+isContained);
?
最后的提示如下:
?
?
甚是不解、、、
?
后来在网上google一番、、、
?
给出如下解释:
RegExp中有一个全局变量lastIndex,它记录待匹配串中下次匹配开始时的字符位置。默认为0,如果匹配成功,则是上次匹配串的长度,如果没有匹配成功,也返回0.
该变量有正则表达式对象的exec和test方法改变,也可以由String对象的replace,match和split等方法来改变,这个需要注意。
?
Also
RegExp Object Properties | Regular Expression Syntax
Applies To: RegExp Object
Requirements
Version 3
Returns the character position where the next match begins in a searched string.
RegExp.lastIndex
The object associated with this property is always the global RegExp object.
Remarks
The lastIndex property is zero-based, that is, the index
of the first character is zero. Its initial value is –1. Its value is
modified whenever a successful match is made.
The lastIndex property is modified by the exec and test methods of the RegExp object, and the match, replace, and split methods of the String object.
The following rules apply to values of lastIndex:
If there is no match, lastIndex is set to -1.
If lastIndex is greater than the length of the string, test and exec fail and lastIndex is set to -1.
If
lastIndex is equal to the length of the string, the regular expression
matches if the pattern matches the empty string. Otherwise, the match
fails and lastIndex is reset to -1.
?
因为在我的正则表达式中使用了全局匹配模式 "g" , 所以用完了之后 必须要重置lastIndex.
或者去掉全局匹配模式的参数 "g" , 这样儿就一致了、、、
O(∩_∩)O~