用正则表达式和雅虎管道替换 html 链接
我的问题与这里的问题类似:
我通过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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
安东尼的警告是,你无法使用正则表达式安全地解析 HTML,所以如果你选择使用 Pipes,请权衡所涉及的风险。
假设您只想替换这个特定的结构,并期望如果源代码中的任何内容发生微妙的变化,它就会被破坏,那么以下内容将足以满足您的目的:
将:替换
为:(
使用 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:
with:
(Using s and i options)