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

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

返回正則表達式match()在Javascript中的位置?

返回正則表達式match()在Javascript中的位置?

精慕HU 2019-11-12 10:49:51
有沒有辦法在Java的regex match()結果的字符串中檢索(開始)字符位置?
查看完整描述

3 回答

?
慕婉清6462132

TA貢獻1804條經驗 獲得超2個贊

exec返回具有index屬性的對象:

var match = /bar/.exec("foobar");

if (match) {

    console.log("match found at " + match.index);

}

對于多個匹配項:


var re = /bar/g,

    str = "foobarfoobar";

while ((match = re.exec(str)) != null) {

    console.log("match found at " + match.index);

}


查看完整回答
反對 回復 2019-11-12
?
手掌心

TA貢獻1942條經驗 獲得超3個贊

這是我想出的:


// Finds starting and ending positions of quoted text

// in double or single quotes with escape char support like \" \'

var str = "this is a \"quoted\" string as you can 'read'";


var patt = /'((?:\\.|[^'])*)'|"((?:\\.|[^"])*)"/igm;


while (match = patt.exec(str)) {

  console.log(match.index + ' ' + patt.lastIndex);

}


查看完整回答
反對 回復 2019-11-12
  • 3 回答
  • 0 關注
  • 1270 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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