Skip to content Skip to sidebar Skip to footer

How To Detect And Get Url On String Javascript

how to detect and get url on string javascript? example : var string = 'hei dude, check this link http:://google.com and http:://youtube.com' how to get result like this from my s

Solution 1:

You input has double :: after http. Not sure if it intentional. If it is then use:

var matches = string.match(/\bhttps?::\/\/\S+/gi);

If only one : is needed then use:

var matches = string.match(/\bhttps?:\/\/\S+/gi);

RegEx Demo

Solution 2:

const string = "hei dude, check this link http:://google.com and http:://youtube.com"const matches = string.match(/\bhttp?::\/\/\S+/gi);
console.log(matches);

Post a Comment for "How To Detect And Get Url On String Javascript"