正则表达式 – 如何在选定字符之后添加任何内容

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

RegEx - How to Add Anything After Selected Characters

问题

我正在尝试将 rel="nofollow noopener" target="_blank" 添加到HTML中的所有链接。

以下是示例HTML代码:

<li><a href="https://somewebsite.com/">Some website</a></li>
<li><a href="https://awesomewebsite.net/">Awesome Website</a></li>
<li><a href="https://coolwebsite.org/">Cool Website</a></li>

<p>Also <a href="https://badsite.com/">check this website</a></p>!

我对正则表达式一无所知,但在阅读了一些资源后,我已经编写了以下正则表达式:

href([^>]*)

这将选择所有链接,但我不确定如何在Notepad++中添加rel="nofollow noopener" target="_blank" 到我已经选择的部分 正则表达式 – 如何在选定字符之后添加任何内容

我正在尝试使用正则表达式将**rel="nofollow noopener" target="_blank"**添加到所有链接中。

英文:

I am trying to add rel="nofollow noopener" target="_blank" to all the links in HTML.

Here's the example HTML code:

<li><a href="https://somewebsite.com/">Some website</a></li>
<li><a href="https://awesomewebsite.net/">Awesome Website</a></li>
<li><a href="https://coolwebsite.org/">Cool Website</a></li>

<p>Also <a href="https://badsite.com/">check this website</a></p>! 

I know nothing about RegEx but after reading some resources I have written

href([^>]*)

Which selects all the links but I'm not sure how would I add rel="nofollow noopener" target="_blank" in Notepad++ after what I've selected 正则表达式 – 如何在选定字符之后添加任何内容

I am trying to add rel="nofollow noopener" target="_blank" to all the links with RegEx

答案1

得分: 0

让我们假设您打开了一个单个的HTML文件,您可以按照以下步骤进行。请小心使用这些选项,并在操作之前创建备份。

  • <kbd>Ctrl</kbd>+<kbd>H</kbd>
  • 查找内容:(href([^&gt;]*)) 或缩写和更好的 (href[^&gt;]*)
  • 替换为:$1 target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;
  • 搜索模式:正则表达式
  • 单击<kbd>Replace All</kbd>(自担风险

这将导致:

正则表达式 – 如何在选定字符之后添加任何内容

对于多个HTML文件,您可能需要尝试使用在文件中查找满足您的需求(<kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>F</kbd>)。

正则表达式 – 如何在选定字符之后添加任何内容

英文:

Lets say you have a single HTML file open you'd go through following steps. Be careful with these options and create a backup beforehand.

  • <kbd>Ctrl</kbd>+<kbd>H</kbd>
  • Find what: (href([^&gt;]*)) or shortened and better (href[^&gt;]*)
  • Replace with: $1 target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;
  • Search mode: Regular expression
  • Click on <kbd>Replace All</kbd> (at your own risk!)

This is resulting in:

正则表达式 – 如何在选定字符之后添加任何内容

For multiple HTML files, you may want to try Find in Files for your needs (<kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>F</kbd>).

正则表达式 – 如何在选定字符之后添加任何内容

huangapple
  • 本文由 发表于 2023年2月19日 02:56:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495672.html
匿名

发表评论

匿名网友

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

确定