复杂(?)正则表达式,用于在<a>大量链接列表中填充标签的"title"属性



我在页面上有一系列<ol>元素。它们的每个<li>都包含文本,后跟文本源,即超链接。

下面是一个示例:

<ol>
<li>One plus one equals two (<a href="https://me.com" target="_blank">Me</a>)</li>
<li>Your &quot;reality&quot;, sir, is lies and balderdash, and I'm delighted to say that I have no grasp of it whatsoever (<a href="https://bvm.com" target="_blank">Baron von Munchausen</a>)</li>
</ol>

我想让每个 href 标签都有一个title属性,该属性是括号源之前的全部文本。所以上面的例子将变成:

<ol>
<li>One plus one equals two (<a title="One plus one equals two" href="https://me.com" target="_blank">Me</a>)</li>
<li>Your &quot;reality&quot;, sir, is lies and balderdash, and I'm delighted to say that I have no grasp of it whatsoever (<a title="Your &quot;reality&quot;, sir, is lies and balderdash, and I'm delighted to say that I have no grasp of it whatsoever" href="https://bvm.com" target="_blank">Baron von Munchausen</a>)</li>
</ol>

如果这甚至可以通过查找/替换正则表达式来实现这一点,那么使用它的最佳正则表达式是什么? – 如果有帮助,我正在使用 VSCode。

提前感谢!

当然

<li>(.*) (<a href

<li>$1 (<a title="$1" href

结果

<ol>
<li>One plus one equals two (<a title="One plus one equals two" href="https://me.com" target="_blank">Me</a>)</li>
<li>Your &quot;reality&quot;, sir, is lies and balderdash, and I'm delighted to say that I have no grasp of it whatsoever (<a title="Your &quot;reality&quot;, sir, is lies and balderdash, and I'm delighted to say that I have no grasp of it whatsoever" href="https://bvm.com" target="_blank">Baron von Munchausen</a>)</li>
</ol>

您必须审查它的双引号

最新更新