Find all matching regex patterns and index of the match in the string

后端 未结 3 776
轮回少年
轮回少年 2020-12-17 00:30

I want to find /AA/ pattern in AA-AA-AA subject string. I need to get the matching string and the position (index) of the match.

I have loo

3条回答
  •  甜味超标
    2020-12-17 01:10

    http://jsfiddle.net/mplungjan/MNXvQ/

    I think this is easier to grasp

    var str = "AAbAAcAAd"
    var re = /(AA)/gi;
    var t="",cnt=0;
    while ((result=re.exec(str))!=null) {
        document.write((cnt++)+":"+result[1]+"
    ") }

    re.lastIndex contains the positions each time

提交回复
热议问题