Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 检测错误的URL_Node.js_Regex_Url - Fatal编程技术网

Node.js 检测错误的URL

Node.js 检测错误的URL,node.js,regex,url,Node.js,Regex,Url,我有一个API,它有时会返回这样的值http://www.trinidadexpress.comhttp://www.trinidadexpress.com/storyimage/TT/20171220/LOCAL/171229994/AR/0/AR-171229994.jpg&MaxW=400 有没有一种方法可以使用node.js来检测这样的URL(基本上在URL中有两个http://地址),如果它有这种模式,它将替换为https://www.wefornews.com/wp-content/

我有一个API,它有时会返回这样的值
http://www.trinidadexpress.comhttp://www.trinidadexpress.com/storyimage/TT/20171220/LOCAL/171229994/AR/0/AR-171229994.jpg&MaxW=400


有没有一种方法可以使用
node.js
来检测这样的URL(基本上在URL中有两个http://地址),如果它有这种模式,它将替换为
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){
警报(“无效url!”);

}
修复API或使用不同的API,这是不正常的。解决源代码问题,不要使用bandaid,希望它“基本上在url中有两个http://”,如果在纯js中,您已经可以使用正则表达式甚至纯字符串搜索来检查,谁需要node.js?