亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

請教和正則的問題

請教和正則的問題

海綿寶寶撒 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 關注
  • 577 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號