Javascript Regexp loop all matches

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

    I agree with Jason that it’d be faster/safer to use an existing Markdown library, but you’re looking for String.prototype.replace (also, use RegExp literals!):

    var Text = "[Text Example][1]\n[1][http: //www.example.com]";
    var rePattern = /\[(.+?)\]\[([0-9]+)\]/gi;
    
    console.log(Text.replace(rePattern, function(match, text, urlId) {
      // return an appropriately-formatted link
      return `${text}`;
    }));

提交回复
热议问题