Javascript Regexp loop all matches

前端 未结 6 2014
名媛妹妹
名媛妹妹 2021-02-06 22:16

I\'m trying to do something similar with stack overflow\'s rich text editor. Given this text:

[Text Example][1]

[1][http://www.example.com]

I

6条回答
  •  自闭症患者
    2021-02-06 22:34

    Here we're using exec method, it helps to get all matches (with help while loop) and get position of matched string.

        var input = "A 3 numbers in 333";
        var regExp = /\b(\d+)\b/g, match;
        while (match = regExp.exec(input))
          console.log("Found", match[1], "at", match.index);
        // → Found 3 at 2 //   Found 333 at 15 
    

提交回复
热议问题