首先,javascript中正则的exec方法返回的既然是数组为什么下面代码的length值为1而不是2,
还有就是exec的lastindex属性为什么是undefined???
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>正则表达式</title>
<style>
</style>
</head>
<body>
<script type="text/javascript">
/*var pattem = new RegExp ("box");
var str = "box";
alert(pattem.test(str)); */
/*var pattem = /box/i;
var str = "Box";
alert(pattem.test(str)); */
/*alert(/box/i.test("Box")) */
var pattem = /box/;
var str = "this is a box,box";
alert(pattem.exec(str).lastIndex);
</script>
</body>
</html>
lastIndex
指的是正则对象的属性,并不是exec
返回的结果的属性。MDN:
If the match succeeds, the exec() method returns an array and updates properties of the regular expression object. The returned array has the matched text as the first item, and then one item for each capturing parenthesis that matched containing the text that was captured.
你确定你正确理解这些话的意思了?
RegExp.exec返回的是结果数组. 不包含lastIndex这一类的东西. 只有截取的字符串.
lastIndex这一类的在RegExp对象内
详细请参阅MDN文档