当内容在 SublimeText 中有特定单词时,使用正则表达式更改 html 的标签

Change tag of html with regex when content has specific word in SublimeText

我有一个 html 有:

<p class="s5">Chapter 1 – General Information</p>
<p class="s5">Section 1 – Example</p>
<p>Some text</p>
<p class="s5">Chapter 2 – Introduction</p>

并且我想替换每个以 Chapter 开头的 <p class="s5"> 标签,用于 <h1> ... </h1>

我如何在 Sublime Text 中使用正则表达式替换来执行它?

您没有指明您使用的是哪个 language/tool,所以这是一个通用的解决方案:

Search: (?<=<p class="s5">)(Chapter[^<]*)
Replace: <h1></h1>

细分:

  • (?<=<p class="s5">)look behind(非消耗断言)<p class="s5">
  • (Chapter[^<]*) 是以 Chapter 开头的文本,直到下一个 <

如果您的工具不理解向后看,您可以使用并替换前面的输入:

Search: <p class="s5">(Chapter[^<]*)
Replace: <p class="s5"><h1></h1>

请注意 languages/tool 因反向引用语法而异; </code> 可能需要改为 <code>