如何更改HTML标签并保留其中的内容

huangapple go评论79阅读模式
英文:

How to change HTML tags preserving its contained content

问题

在多个HTML文件中,我有217个实例:

<p class="quote">“许多不同的引用。”</p>

我想要批量将它们更改为:

<blockquote>
  <p>“许多不同的引用。”</p>
</blockquote>

我曾考虑使用正则表达式,直到我看到了这个答案,它不鼓励使用正则表达式。但这个答案相当古老,所以我想知道是否有新的最佳方法。

我尝试过正则表达式,但不确定是否是最有效的方法。我正在使用vscodium,但没有立即找到相关的扩展。

英文:

Across multiple html files I have 217 instances of:

<p class="quote">“Lots of different quotes.”</p>

And want to bulk change them into:

<blockquote>
  <p>“Lots of different quotes.”</p>
</blockquote>

I was thinking of using regular expressions, until I came across this answer, which discourages the use of regex. But the answer is quite old so I wonder if there is a new best approach.

I tried regex but I am not sure whether that is the most efficient method. I am using vscodium, but didn't immediately find relevant extensions.

答案1

得分: 0

使用VSCodium,以下正则表达式可以捕获大多数情况并进行替换:

<p class="quote">([^<]+)</p>

或者使用以下正则表达式以包括嵌套标签:

<p class="quote">((?:[^<]+|<(?!/p>))*)</p>

替换为:

<blockquote>\n\t\t\t<p>$1</p>\n\t\t</blockquote>
英文:

Using VSCodium the following regular expression captured most cases by replacing:

<p class="quote">([^<]+)</p>

or the following to include nested tags

<p class="quote">((?:[^<]+|<(?!/p>))*)</p>

with

<blockquote>\n\t\t\t<p>$1</p>\n\t\t</blockquote>

huangapple
  • 本文由 发表于 2023年8月4日 01:20:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830318.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定