-5

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!

  • 1
    Urge to kill rising. On a more serious note, every language I can think of has a URL-parsing method for this type of thing. What language are you using? – Marty May 05 '14 at 06:28
  • This is essentially a duplicate of [How to replace plain URLs with links?](http://stackoverflow.com/questions/37684/how-to-replace-plain-urls-with-links), which explains why regular expressions are a bad idea for this kind .f task. – Dan Dascalescu Oct 11 '16 at 03:52

1 Answers1

0

As Marty says, it all depends on which language you are using. You could do it in JavaScript, like so:

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]);

Here's a demo

I got the regex from this thread: JavaScript Regex to match a URL in a field of text

Community
  • 1
  • 1
James Hibbard
  • 16,490
  • 14
  • 62
  • 74
  • In my project url can change dynamically and your sol work for above link but not working for other large links like an example:-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 May 05 '14 at 12:14
  • Works for me - [fiddle](http://jsfiddle.net/hibbard_eu/6wD2S/2/) – James Hibbard May 05 '14 at 17:51