2014-05-05 160 views
-5

找到例如URL我有一個例子鏈接: - https://www.google.co.in/search?q=web+service+urls&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a&channel=sb&gfe_rd=cr&ei=ex5iU-6CLMeW8QezvoCgAg正則表達式從字符串

我需要一個正則表達式extact從文本字符串,完整的URL。 謝謝!

+1

敦促殺死上升。更爲嚴肅的說法是,我能想到的每種語言都有這種類型的URL解析方法。你在用什麼語言? – Marty

+0

這實質上是[如何用鏈接替換普通URL?](http://stackoverflow.com/questions/37684/how-to-replace-plain-urls-with-links)的副本,這解釋了爲什麼正則表達式對於這種.f任務來說是一個糟糕的主意。 –

回答

0

正如馬蒂所說,這一切都取決於您使用的是哪種語言。 你能做到這一點在JavaScript中,像這樣:

var myString = "i have a example link:- https://www.google.co.in/search?q=web+service+urls&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a&channel=sb&gfe_rd=cr&ei=ex5iU-6CLMeW8QezvoCgAg i need a regex to extact that full url from text string. thanks!"; 
var myRegexp = /(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&:\/~+#-]*[\[email protected]?^=%&\/~+#-])?/ 
var match = myRegexp.exec(myString); 

alert(match[0]); 

這裏有一個demo

我從這個線程的正則表達式:JavaScript Regex to match a URL in a field of text

+0

在我的項目中,url可以動態更改,而您的sol可以用於上面的鏈接,但不適用於其他大型鏈接,如示例:-https://www.google.co.in/search?q = html + encode + javascript&oq = html + encode + ja&aqs = chrome.1.69i57j0l5.11047j0j7&sourceid = chrome&es_sm = 122&ie = UTF-8#q = urlencode%20javascript – user3603180

+0

適用於我 - [fiddle](http://jsfiddle.net/hibbard_eu/6wD2S/2/) –

+0

是的,它的工作。謝謝 – user3603180