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
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);