Javascript Regexp loop all matches

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

    Here's somw small example I hope you can find useful. \number is used in regex to refer a group match number and $number is used in the replace function to refer group results so you can enforce that numbers will be the same with something like that if your text is

    [Text Example][1]\n[1][http://www.example.com]
    

    it will match and if it is

    [Text Example][1]\n[2][http://www.example.com]
    

    it won't

    var re = /\[(.+?)\]\[([0-9]+)\s*.*\s*\[(\2)\]\[(.+?)\]/gi;
    var str = '[Text Example][1]\n[1][http://www.example.com]';
    var subst = '$1';
    
    var result = str.replace(re, subst);
    console.log(result);

提交回复
热议问题