【问题标题】:jQuery Check if string is in RegExpjQuery 检查字符串是否在 RegExp 中
【发布时间】:2016-04-19 11:59:54
【问题描述】:

我想检查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);

【问题讨论】:

  • 你的意思是fileExt in thisFile?
  • 从模式中删除(\.|\/),或使其成为可选:[.\/]?
  • fileExt 是文件扩展名,如jpg 等。

标签: javascript jquery regex


【解决方案1】:

您可以使用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');

【讨论】:

    【解决方案2】:

    检查这个

    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");
      }
      

    【讨论】:

      【解决方案3】:

      也许这会有所帮助

      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
      }
      

      【讨论】:

        【解决方案4】:

        不必创建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
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-01-02
          • 2018-04-06
          • 2011-12-13
          • 1970-01-01
          • 2023-04-07
          • 1970-01-01
          • 2013-01-09
          • 2018-12-16
          相关资源
          最近更新 更多