If i understand you correctly, you need this:
var re = /(#table\d+)|\((\w{1,2})(\))/g;
var str="<p id="para"> demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo<a href="#table1(t1)" id="ytD2F">table1</a> test demo test demo test demo test demo test demo <a href="#table2(t2)" id="ytD2F">table2</a> demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test </p>";
var subst="$2";
var result = str.replace(re, subst);
console.log(result);
Regex101 demo: https://regex101.com/r/uP6uY7/1
1
solved replace href value in dom variable [duplicate]