I am working in an application where I am facing a problem while text formatting(bold, italics and underlined). I am making an array by splitting the whole text with #b#, #i# and #u#. But for few scenarios it is difficult to split like that. I am having another circumstances where I need to check the words like '%% word' to be separated also. Few examples are :
1. A #i#sample#/i# %%text #b#with%% text format#/b#
2. %%#b#A#/b#%%|%%#b#B#/b#%% will be good.
In all case I need a array like the following -
["A"," ","<i>sample</i>"," ","%%text<b>with</b>%%"," ","<b>text format</b>"]
["%%<b>A</b>%%","|","%%<b>B</b>%%"," ","will be good."] //Space excluded
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 >
.