我有一个API,有时返回像这样的http://www.trinidadexpress.comhttp://www.trinidadexpress.com/storyimage/TT/20171220/LOCAL/171229994/AR/0/AR-171229994.jpg&MaxW=400
值
有没有办法使用node.js
来检测这样的URL(基本上在URL中有2个http://)如果它有这个模式替换它将是一个URL,如https://www.wefornews.com/wp-content/uploads/2017/01/news-3.jpg
匹配您想要查找的字符串的正则表达式非常简单:/https?:\/\//g
(查找字符“http”,s?
表示“s”中的一个或不表示,以及/
转义为“://”)
要检查字符串是否有多个实例,请使用String.prototype.match
并检查返回数组的长度。
这是一个例子:
var str = "http://www.trinidadexpress.comhttp://www.trinidadexpress.com/storyimage/TT/20171220/LOCAL/171229994/AR/0/AR-171229994.jpg&MaxW=400";
if (str.match(/https?:\/\//g).length > 1) {
alert("invalid url!");
}