如何检测和获取字符串 javascript 上的 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33211233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:13:20  来源:igfitidea点击:

how to detect and get url on string javascript

javascriptregexstring

提问by monoy suronoy

how to detect and get url on string javascript?

如何检测和获取字符串javascript上的url?

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?

怎么办?

回答by anubhava

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

您输入具有双重::之后http。不确定是不是故意的。如果是,则使用:

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

If only one :is needed then use:

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

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

RegEx Demo

正则表达式演示

回答by QasimRamzan

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

var 匹配 = string.match(/\bhttp?:://\S+/gi); 控制台日志(匹配);

Ans is ["http:://google.com", "http:://youtube.com"]

答案是 ["http:://google.com", "http:://youtube.com"]