【发布时间】:2019-04-16 23:57:55
【问题描述】:
我正在开发一个自定义解析函数,我需要在其中解析一个字符串
多次重复出现 && 与 <b> 和 %% 与 <i> 标记。
我试过这个正则表达式。
html = html.replace(/[&][&](.+?)[&][&]/g, '<b>$1</b>');
html = html.replace(/[%][%](.+?)[%][%]/g, '<i>$1</i>');
上面的替换对我的意思是单个组很好
"&&This&& is a &&bold string&&" to "this is a bold string"
这给出了奇怪的结果,我有重复的字符串
&&&&This&&&& is a &&bold string&&
到<b>&&This</b>&& is a <b>bold String</b>
在解析结束组和开始组时需要帮助以将其替换为适当的 html 标记,例如
致<b><b>This</b></b> is a <b>bold String</b>
如果可能的话,只用一个<b>标签替换它
致<b>This</b> is a <b>bold String</b>
【问题讨论】:
标签: javascript regex html-parsing regex-group