为了账号安全,请及时绑定邮箱和手机立即绑定

请教和正则的问题

请教和正则的问题

海绵宝宝撒 2018-09-06 15:09:53
首先,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>
查看完整描述

1 回答

?
叮当猫咪

TA贡献1776条经验 获得超12个赞

lastIndex指的是正则对象的属性,并不是exec返回的结果的属性。

var reg = /box/g;var str = 'this is a box , box ';while(reg.exec(str))
    console.log(reg.lastIndex);

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.

你确定你正确理解这些话的意思了?


查看完整回答
反对 回复 2018-10-09
  • 1 回答
  • 0 关注
  • 581 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号