我想检查fileExt
是否在avoidExt
中,并在if...else
声明中使用它。
var thisFile=$(this).val(); //returns "file.jpg"
var fileExt = thisFile.replace(/^.*\./, ''); //return "jpg"
var avoidExt= new RegExp(/(\.|\/)(bat|exe|cmd|sh|php|pl|cgi|386|dll|com|torrent|js|app|jar|pif|vb|vbscript|wsf|asp|cer|csr|jsp|drv|sys|ade|adp|bas|chm|cpl|crt|csh|fxp|hlp|hta|inf|ins|isp|jse|htaccess|htpasswd|ksh|lnk|mdb|mde|mdt|mdw|msc|msi|msp|mst|ops|pcd|prg|reg|scr|sct|shb|shs|url|vbe|vbs|wsc|wsf|wsh)$/i);
答案 0 :(得分:0)
您可以使用 RegExp.prototype.test()
var thisFile = "file.jpg";
var fileExt = thisFile.replace(/^.*\./, ''); //return "jpg"
var avoidExt = new RegExp(/(\.|\/)(bat|exe|cmd|sh|php|pl|cgi|386|dll|com|torrent|js|app|jar|pif|vb|vbscript|wsf|asp|cer|csr|jsp|drv|sys|ade|adp|bas|chm|cpl|crt|csh|fxp|hlp|hta|inf|ins|isp|jse|htaccess|htpasswd|ksh|lnk|mdb|mde|mdt|mdw|msc|msi|msp|mst|ops|pcd|prg|reg|scr|sct|shb|shs|url|vbe|vbs|wsc|wsf|wsh)$/i);
console.log(avoidExt.test('.' + fileExt) ? 'fileExt is in' : 'fileExt is not in');

更新: IF...ELSE
格式
var thisFile = "file.jpg";
var fileExt = thisFile.replace(/^.*\./, ''); //return "jpg"
var avoidExt = new RegExp(/(\.|\/)(bat|exe|cmd|sh|php|pl|cgi|386|dll|com|torrent|js|app|jar|pif|vb|vbscript|wsf|asp|cer|csr|jsp|drv|sys|ade|adp|bas|chm|cpl|crt|csh|fxp|hlp|hta|inf|ins|isp|jse|htaccess|htpasswd|ksh|lnk|mdb|mde|mdt|mdw|msc|msi|msp|mst|ops|pcd|prg|reg|scr|sct|shb|shs|url|vbe|vbs|wsc|wsf|wsh)$/i);
if (avoidExt.test('.' + fileExt))
console.log('fileExt is in')
else
console.log('fileExt is not in');

或者无需使用正则表达式提取扩展名,您可以执行以下操作
var thisFile = "file.jpg";
var avoidExt = new RegExp(/(\.|\/)(bat|exe|cmd|sh|php|pl|cgi|386|dll|com|torrent|js|app|jar|pif|vb|vbscript|wsf|asp|cer|csr|jsp|drv|sys|ade|adp|bas|chm|cpl|crt|csh|fxp|hlp|hta|inf|ins|isp|jse|htaccess|htpasswd|ksh|lnk|mdb|mde|mdt|mdw|msc|msi|msp|mst|ops|pcd|prg|reg|scr|sct|shb|shs|url|vbe|vbs|wsc|wsf|wsh)$/i);
console.log(avoidExt.test(thisFile) ? 'fileExt is in' : 'fileExt is not in');

答案 1 :(得分:0)
检查一下
var thisFile=$(this).val();
var fileExt = thisFile.replace(/^.*\./, '');
var avoidExt= new RegExp(/(\.|\/)(bat|exe|cmd|sh|php|pl|cgi|386|dll|com|torrent|js|app|jar|pif|vb|vbscript|wsf|asp|cer|csr|jsp|drv|sys|ade|adp|bas|chm|cpl|crt|csh|fxp|hlp|hta|inf|ins|isp|jse|htaccess|htpasswd|ksh|lnk|mdb|mde|mdt|mdw|msc|msi|msp|mst|ops|pcd|prg|reg|scr|sct|shb|shs|url|vbe|vbs|wsc|wsf|wsh)$/i);
if(fileExt.test(avoidExt)){
console.log("EXIST");
}else{
console.log("NOT EXIST");
}

答案 2 :(得分:0)
也许这会有所帮助
var thisFile = $(this).val();
// var fileExt = thisFile.replace(/^.*\./, ''); doesn't need this line
var avoidExt= new RegExp(/(\.|\/)(bat|exe|cmd|sh|php|pl|cgi|386|dll|com|torrent|js|app|jar|pif|vb|vbscript|wsf|asp|cer|csr|jsp|drv|sys|ade|adp|bas|chm|cpl|crt|csh|fxp|hlp|hta|inf|ins|isp|jse|htaccess|htpasswd|ksh|lnk|mdb|mde|mdt|mdw|msc|msi|msp|mst|ops|pcd|prg|reg|scr|sct|shb|shs|url|vbe|vbs|wsc|wsf|wsh)$/i);
if(avoidExt.test(thisFile)){
// exist
}else{
// doesn't exist
}
答案 3 :(得分:0)
没有必要创建RegExp
对象,您可以使用正则表达式文字:
var fileExt = $(this).val().replace(/^.*\./, '');
var avoidExt = /(bat|exe|cmd|sh|php|pl|cgi|386|dll|com|torrent|js|app|jar|pif|vb|vbscript|wsf|asp|cer|csr|jsp|drv|sys|ade|adp|bas|chm|cpl|crt|csh|fxp|hlp|hta|inf|ins|isp|jse|htaccess|htpasswd|ksh|lnk|mdb|mde|mdt|mdw|msc|msi|msp|mst|ops|pcd|prg|reg|scr|sct|shb|shs|url|vbe|vbs|wsc|wsf|wsh)/i;
if(fileExt.text(avoidExt)){
// exist
} else {
// doesn't exist
}