正则表达式模式从字符串中查找示例url

时间:2014-05-05 06:26:39

标签: regex

我有一个示例链接: - 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

我需要一个正则表达式从文本字符串中提取完整的URL。 谢谢!

1 个答案:

答案 0 :(得分: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.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?/
var match = myRegexp.exec(myString);

alert(match[0]);

这是demo

我从这个帖子中得到了正则表达式:JavaScript Regex to match a URL in a field of text