我掙扎,試圖linkfiy鏈接帶或不帶 「WWW」/ 「HTTP」正則表達式Linkify:WWW,NONWWW,HTTP,nonHTTP
這是我得到:
noProtocolUrl = /\b((?:www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»「」‘’]))/g,
httpOrMailtoUrl = /\b((?:[a-z][\w-]+:)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»「」‘’]))/gi,
linkifier = function (html) {
return FormatLink(html
.replace(noProtocolUrl, '<a href="<``>://$1" rel="nofollow external" class="external_link">$1</a>') // NOTE: we escape `"http` as `"<``>` to make sure `httpOrMailtoUrl` below doesn't find it as a false-positive
.replace(httpOrMailtoUrl, '<a href="$1" rel="nofollow external" class="external_link">$1</a>')
.replace(/"<``>/g, '"http')); // reinsert `"http`
這是除了與http://的簡單鏈接都是通過兩次鏈接處理獲得的。
http://google.com將成爲兩個環節: HTTTP://和http://google.com
關於如何解決此問題的任何想法?
謝謝!
編輯
嗯,我懂了工作以外的任何鏈接不鏈接HTTP *和** WWW像bit.ly/foo
如果有人知道如何捕捉那些鏈接也是如此,不客氣。
var noProtocolUrl = /(^|["'(\s]|<)(www\..+?\..+?)((?:[:?]|\.+)?(?:\s|$)|>|[)"',])/g,
httpOrMailtoUrl = /\b((?:[a-z][\w-]+:)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»「」‘’]))/gi,
linkifier = function (html) {
return FormatLink(html
.replace(noProtocolUrl, '$1<a href="<``>://$2" rel="nofollow external" class="external_link">$2</a>$3') // NOTE: we escape `"http` as `"<``>` to make sure `httpOrMailtoUrl` below doesn't find it as a false-positive
.replace(httpOrMailtoUrl, '<a href="$1" rel="nofollow external" class="external_link">$1</a>')
.replace(/"<``>/g, '"http')); // reinsert `"http`
},
查看http://stackoverflow.com/questions/247479/jquery-text-to-link-script – Nathan 2011-05-18 00:44:07
彌敦道,問題是我正在使用更新的正則表達式(http://daringfireball.net/2010/ 07/improved_regex_for_matching_urls)將URL與括號等進行匹配。 – Santiago 2011-05-18 00:53:45