Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.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
Javascript 如何验证regex模式以检查mimetype?_Javascript_Regex - Fatal编程技术网

Javascript 如何验证regex模式以检查mimetype?

Javascript 如何验证regex模式以检查mimetype?,javascript,regex,Javascript,Regex,这里我尝试验证SVG图像mime类型。即使我正在传递有效的内容类型,它也会失败。有人能告诉我这个正则表达式有什么问题吗 const mimetypes=/image\/png | image\/jpeg | imagessvg+xml | image\/gif | image\/svg+xml/; var result=mimetypes.test('image/svg+xml') console.log(result)您必须替换+符号,因为它在正则表达式中具有含义: const mimet

这里我尝试验证SVG图像mime类型。即使我正在传递有效的内容类型,它也会失败。有人能告诉我这个正则表达式有什么问题吗

const mimetypes=/image\/png | image\/jpeg | imagessvg+xml | image\/gif | image\/svg+xml/;
var result=mimetypes.test('image/svg+xml')

console.log(result)
您必须替换
+
符号,因为它在正则表达式中具有含义:

const mimetypes=/image\/png | image\/jpeg | imagessvg\+xml | image\/gif | image\/svg\+xml/;
var result=mimetypes.test('image/svg+xml')

log(result)
您也应该转义
+
,它是正则表达式的特殊字符

const mimetypes=/image\/png | image\/jpeg | imagessvg\+xml | image\/gif | image\/svg\+xml/;
var result=mimetypes.test('image/svg+xml')

log(result)
+在正则表达式中是一个量词,因此您还必须转义+符号

const mimetypes=/image\/png | image\/jpeg | imagessvg\+xml | image\/gif | image\/svg\+xml/;
var result=mimetypes.test('image/svg+xml')

console.log(result)
您正在对照一组固定字符串检查一个固定字符串。你根本不需要正则表达式

const mimetypes = ['image/png', 'image/jpeg', 'image/svg+xml', 'image/gif'];

var result = mimetypes.includes('image/svg+xml')