如何在 JavaScript 中检测和获取字符串中的 URL?
示例:
var string = "hei dude, check this link http:://google.com and http:://youtube.com"
如何从我的字符串中获得类似这样的结果:var result = ["http:://google.com", "http:://youtube.com"]
怎么做呢?
如何在 JavaScript 中检测和获取字符串中的 URL?
示例:
var string = "hei dude, check this link http:://google.com and http:://youtube.com"
如何从我的字符串中获得类似这样的结果:var result = ["http:://google.com", "http:://youtube.com"]
怎么做呢?
您的输入在 http
后面有双重冒号::
。不确定这是否是故意的。如果确实是,请使用以下内容:
var matches = string.match(/\bhttps?::\/\/\S+/gi);
如果只需要一个冒号:
,则使用:
var matches = string.match(/\bhttps?:\/\/\S+/gi);
const string = "hei dude, check this link http:://google.com and http:://youtube.com"
const matches = string.match(/\bhttp?::\/\/\S+/gi);
console.log(matches);
http
后使用了双重::
,所以它们并不是格式良好的 URL。 - anubhava