2010-01-12 76 views
1

我的問題是一個類似於此:更換用正則表達式和雅虎管道HTML鏈接

Regular expression on Yahoo! pipes

我有我的Gmail狀態迷上了通過FriendFeed的嘰嘰喳喳,但不幸的是,他們截斷鏈接文本,而且我的鏈接一旦到達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> 

感謝您的幫助!

+1

不解析HTML與正則表達式http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – 2010-01-12 20:20:20

回答

0

Anthony的警告標誌,你不能​​用正則表達式安全地解析HTML,所以如果你選擇使用Pipes,請權衡所涉及的風險。

假設你要替換隻有這個特定的結構,並期望它打破如果巧妙地源改變什麼,下面的工作不夠好,你的目的:

取代:

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

用:

$1$2$3$2$4 

(使用小號選項)