Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JavaScript进行文本解析和格式化_Javascript_Jquery_Text_Text Parsing_Text Formatting - Fatal编程技术网

使用JavaScript进行文本解析和格式化

使用JavaScript进行文本解析和格式化,javascript,jquery,text,text-parsing,text-formatting,Javascript,Jquery,Text,Text Parsing,Text Formatting,我在一个应用程序中工作,在使用文本格式(粗体、斜体和下划线)时遇到问题。我用#b#,#I#和#u#将整个文本分割成一个数组。但在少数情况下,很难像那样拆分。我遇到另一种情况,需要检查“%”等字是否也要分隔。例如: 1. A #i#sample#/i# %%text #b#with%% text format#/b# 2. %%#b#A#/b#%%|%%#b#B#/b#%% will be good. 在所有情况下,我需要一个如下所示的数组- ["A"," ","<i>sample&

我在一个应用程序中工作,在使用文本格式(粗体、斜体和下划线)时遇到问题。我用#b#,#I#和#u#将整个文本分割成一个数组。但在少数情况下,很难像那样拆分。我遇到另一种情况,需要检查“%”等字是否也要分隔。例如:

1. A #i#sample#/i# %%text #b#with%% text format#/b#
2. %%#b#A#/b#%%|%%#b#B#/b#%% will be good.
在所有情况下,我需要一个如下所示的数组-

["A"," ","<i>sample</i>"," ","%%text<b>with</b>%%"," ","<b>text format</b>"]

["%%<b>A</b>%%","|","%%<b>B</b>%%"," ","will be good."] //Space excluded
[“A”、“”、“sample”、“”、“%%text与%%”、“”、“文本格式”]
[“%%A%%”、“|”、“%%B%%”、“将很好。”]//不包括空格
var s=“A#i#sample#/i#%%text#b#带%%text format#/b#”,
s1=“%#b#A#/b#%%b#%%b#/b#%%就好了。”;
s、 替换(/#(.*?)#/g,“”)//“文本格式为%%text的示例%%text”
s1.替换(/#(.*?)#/g,“”)//“%%A%%|%%B%%将很好。”
您可以使用捕获组将
#
替换为相应的括号

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."