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,
- "i've navigated www.facebook.com";
- "i've navigated www.facebook.com , www.google.com";
- "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
Post a Comment