Javascript Regexp loop all matches

前端 未结 6 1932
名媛妹妹
名媛妹妹 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:32

    I managed to do it in the end with this:

    var Text = "[Text Example][1]\n[1][http: //www.example.com]";
    // Find resource links
    reg = new RegExp(
      "\\[(.+?)\\]\\[([0-9]+)\\]",
      "gi");
    var result;
    while (result = reg.exec(Text)) {
      var LinkText = result[1];
      var Match = result[0];
      var LinkID = result[2];
      var FoundURL = new RegExp("\\[" + LinkID + "\\]\\[(.+?)\\]", "g").exec(Text);
      Text = Text.replace(Match, '' + LinkText + '');
    }
    console.log(Text);

提交回复
热议问题