我想将以下内容与多个捕获组匹配:
Definition 1
: This is the definition text that described the term. Can have markdown formatting and
multiple lines.
Definition 2
: This is the definition text with **markdown**.`code`
我还想用以下文本(HTML定义列表)替换它:
<dl>
<dt>Definition 1</dt>
<dd>This is the definition text that described the term. Can have markdown formatting and
multiple lines.</dd>
<dt>Definition 1</dt>
<dd>This is the definition text with **markdown**.`code`</dd>
</dl>
答案 0 :(得分:1)
您可以分两步完成此操作:
dt
和dd
代码使用以下方式执行搜索:
(.*)\R: ((?:.+(?:\R|$))*?)(?=\R|.*\R:|$)/g
并替换为:
<dt>$1</dt>\n<dd>$2</dd>\n
请参阅regex tester。
dl
代码使用上一次替换的结果并执行以下搜索:
/(<dt>.*?<\/dd>(?!\s*<dt>))/gs
并替换为:
<dl>\n$1\n</dl>
请参阅regex tester。
如果不支持\R
转义,请改用\n
可能需要将后引用$1
,$2
更改为\1
,\2
,具体取决于您的正则表达式引擎。