2014-05-05 161 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