Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to detect and get url on string javascript [duplicate]

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 string :

var result = ["http:://google.com", "http:://youtube.com"]

how do that?

like image 730
monoy suronoy Avatar asked Oct 19 '15 09:10

monoy suronoy


People also ask

How do I find the URL of text?

If you want to find a webpage where one term appears in the text of that page and another term appears elsewhere on the page, like the title or URL, then type in that first term followed by intext: followed immediately by the other term.


2 Answers

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

like image 111
anubhava Avatar answered Oct 03 '22 23:10

anubhava


const string = "hei dude, check this link http:://google.com and http:://youtube.com"
const matches = string.match(/\bhttp?::\/\/\S+/gi);
console.log(matches);
like image 28
QasimRamzan Avatar answered Oct 04 '22 00:10

QasimRamzan