how to detect and get url on string javascript [du

2020-06-12 03:25发布

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?

2条回答
乱世女痞
2楼-- · 2020-06-12 03:50

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

查看更多
ゆ 、 Hurt°
3楼-- · 2020-06-12 03:51

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

查看更多
登录 后发表回答