var s = "A #i#sample#/i# %%text #b#with%% text format#/b#",
s1 = "%%#b#A#/b#%%|%%#b#B#/b#%% will be good.";
s.replace(/#(.*?)#/g, "<$1>"); //"A <i>sample</i> %%text <b>with%% text format</b>"
s1.replace(/#(.*?)#/g, "<$1>"); //"%%<b>A</b>%%|%%<b>B</b>%% will be good."
You can use capture groups to replace the #
with appropriate brackets <
and >
.