【问题标题】:replacing html link with regex and yahoo pipes用正则表达式和雅虎管道替换 html 链接
【发布时间】:2010-01-12 20:07:21
【问题描述】:

我的问题和这里的类似:

Regular expression on Yahoo! pipes

我通过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> 

感谢您的帮助!

【问题讨论】:

标签: regex yahoo-pipes


【解决方案1】:

Anthony 的警告是,您不能使用正则表达式安全地解析 HTML,所以如果您选择使用 Pipes,请权衡所涉及的风险。

假设您只想替换这个特定的结构,并期望它在源代码中的任何细微变化时中断,以下内容将足以满足您的目的:

替换:

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

与:

$1$2$3$2$4

(使用 si 选项)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多