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

后端 未结 3 787
轮回少年
轮回少年 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 00:57

    exec() only returns a single match. To get all matches with a g​lobal regexp, you have to call it repeatedly, eg.:

    var match, indexes= [];
    while (match= r.exec(value))
        indexes.push([match.index, match.index+match[0].length]);
    

提交回复
热议问题