我有一个正则表达式,用于检查字符串是否为URL:
const regex = /(http|https):\/\/(\w+:{0,1}\w*)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%!\-\/]))?/
我试过了:
const regex = '/(http|https):\/\/(\w+:{0,1}\w*)?(\S+)(:[0-9]+)?' + // eslint-disable-line
'(\/|\/([\w#!:.?+=&%!\-\/]))?/' // eslint-disable-line
但正则表达式不再匹配了。同样的:
const regex = new RegExp(['/(http|https):\/\/(\w+:{0,1}\w*)?(\S+)(:[0-9]+)?', // eslint-disable-line
'(\/|\/([\w#!:.?+=&%!\-\/]))?/'].join('')) // eslint-disable-line
这样做的正确方法是什么?
修改
示例字符串:http://vrcam-10065475.image.myqcloud.com/7a88620cb5ec7eb90164.jpg?imageView2/2/w/600/h/600/q/100/format/jpg
答案 0 :(得分:3)
试试:
var regex = RegExp(
"(http|https)://(\\w+:{0,1}\\w*)?(\\S+)(:[0-9]+)?" +
"(/|/([\\w#!:.?+=&%!-/]))?"
);