用正则表达式和雅虎管道替换 html 链接

发布于 2024-08-17 14:25:31 字数 1151 浏览 7 评论 0原文

我的问题与这里的问题类似:

Yahoo! 上的正则表达式管道

我通过friendfeed将我的gmail状态连接到twitter,但不幸的是,他们截断了链接文本,并且我的链接一旦到达twitter就无法工作。我需要能够接受这个:

 <div style="margin-top:2px;color:black;">/good jquery tips 
   <a rel="nofollow" style="text-decoration:none;color:#00c;" target="_blank"  href="http://james.padolsey.com/javascript/things-you-may-not-know-about-jquery/" title="http://james.padolsey.com/javascript/things-you-may-not-know-about-jquery/">
      http://james.padolsey.com/javascr...
   </a>
</div> 

并将截断的链接替换为 href 属性,所以它看起来像这样:

 <div style="margin-top:2px;color:black;">/good jquery tips 
   <a rel="nofollow" style="text-decoration:none;color:#00c;" target="_blank"  href="http://james.padolsey.com/javascript/things-you-may-not-know-about-jquery/" title="http://james.padolsey.com/javascript/things-you-may-not-know-about-jquery/">
      http://james.padolsey.com/javascript/things-you-may-not-know-about-jquery/
   </a>
</div> 

感谢您的帮助!

my question is similar to the one here:

Regular expression on Yahoo! pipes

i have my gmail status hooked up to twitter through friendfeed, but unfortunately, they truncate the link text, and my links aren't working once they get to twitter. I need to be able to take this:

 <div style="margin-top:2px;color:black;">/good jquery tips 
   <a rel="nofollow" style="text-decoration:none;color:#00c;" target="_blank"  href="http://james.padolsey.com/javascript/things-you-may-not-know-about-jquery/" title="http://james.padolsey.com/javascript/things-you-may-not-know-about-jquery/">
      http://james.padolsey.com/javascr...
   </a>
</div> 

and replace the truncated link with the href attribute, so it looks like this:

 <div style="margin-top:2px;color:black;">/good jquery tips 
   <a rel="nofollow" style="text-decoration:none;color:#00c;" target="_blank"  href="http://james.padolsey.com/javascript/things-you-may-not-know-about-jquery/" title="http://james.padolsey.com/javascript/things-you-may-not-know-about-jquery/">
      http://james.padolsey.com/javascript/things-you-may-not-know-about-jquery/
   </a>
</div> 

thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

归途 2024-08-24 14:25:31

安东尼的警告是,你无法使用正则表达式安全地解析 HTML,所以如果你选择使用 Pipes,请权衡所涉及的风险。

假设您只想替换这个特定的结构,并期望如果源代码中的任何内容发生微妙的变化,它就会被破坏,那么以下内容将足以满足您的目的:

将:替换

(<a\s[^>]*href=")(.*?)("[^>]*>).*?(</a>)

为:(

$1$2$3$2$4

使用 s>i 选项)

Anthony's warning stands, you can't parse HTML safely with regexes, so please weigh up the risks involved if you choose to use Pipes.

Assuming you want to replace only this specific structure, and expect it to break if anything in the source changes subtly, the following will work well enough for your purposes:

replace:

(<a\s[^>]*href=")(.*?)("[^>]*>).*?(</a>)

with:

$1$2$3$2$4

(Using s and i options)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文