我有一个正则表达式来检查字符串是否是 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
最佳答案
尝试一下:
var regex = RegExp(
"(http|https)://(\\w+:{0,1}\\w*)?(\\S+)(:[0-9]+)?" +
"(/|/([\\w#!:.?+=&%!-/]))?"
);
关于javascript - 如何正确地将这个正则表达式分成两行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40648632/