如何在字符串javascript上检测并获取url

mon*_*noy 7 javascript regex string

如何在字符串javascript上检测并获取url?

例如:

var string = "hei dude, check this link http:://google.com and http:://youtube.com"
Run Code Online (Sandbox Code Playgroud)

如何从我的字符串中得到这样的结果:

var result = ["http:://google.com", "http:://youtube.com"]
Run Code Online (Sandbox Code Playgroud)

怎么办?

anu*_*ava 23

您输入具有双重::之后http.不确定是否有意.如果是那么使用:

var matches = string.match(/\bhttps?::\/\/\S+/gi);
Run Code Online (Sandbox Code Playgroud)

如果只:需要一个,那么使用:

var matches = string.match(/\bhttps?:\/\/\S+/gi);
Run Code Online (Sandbox Code Playgroud)

RegEx演示

  • 伟大的@anubhava谢谢你 (2认同)

Qas*_*zan 6

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