javascript - Jquery check if String consists of any URL and get it -


i want url string unfamiliar use of regex or such methods.

for example have 3 strings,

  1. "i've navigated www.facebook.com";
  2. "i've navigated www.facebook.com , www.google.com";
  3. "i've navigated https://www.facebook.com ;

in case : should "www.facebook.com" url extracted first string.

all want first url inside string can make link preview using api found. struggling url using javascript or jquery. string gotten textbox , want url on keyup.

you can use function extract first url found in given string:

function getfirsturl(string) {     var pattern = /(https?:\/\/)?(www\.)?[-a-za-z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-za-z0-9@:%_\+.~#?&//=]*)/;     var match = string.match(pattern);     return match[0]; }  var string = "i've nagivated www.facebook.com , www.google.com"; var url = getfirsturl(string); // url === 'www.facebook.com' 

i got regex pattern this answer , modified make https:// part optional.


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -