【问题标题】:dynamic replace the RegExp object without slashes动态替换不带斜线的 RegExp 对象
【发布时间】:2012-08-16 14:13:54
【问题描述】:

我对动态创建的RegExp 对象有问题(在file upload plugin 中)

这是插件的初始化代码:

$('#fileupload').fileupload('option', {
    acceptFileTypes: /(\.|\/)(doc|pdf)$/i
});

该代码更改了正则表达式:

$('#files-list').change(function() {
   $('#fileupload').fileupload(
            'option',
            'acceptFileTypes',
            new RegExp('(\.|\/)('+$(this).find(":selected").attr('f-ext')+')$/i')
                );
            });

它会在该正则表达式的开头和结尾添加 / 字符。

见下图。第 1 和第 2 行是插件初始化后的第 3 和第 4 行。这会导致文件名验证失败。

如何解决?

【问题讨论】:

    标签: jquery regex


    【解决方案1】:

    要使用不区分大小写的修饰符,您应该执行下一个

    new RegExp( regexp_expression , 'i' );
    

    【讨论】:

      【解决方案2】:

      JS 正则表达式的修饰符,当在构造函数中提供时,应单独指定:

      ...new RegExp('(\.|\/)('+$(this).find(":selected").attr('f-ext')+')$', 'i');
      

      general syntax 是:

      var newRegex = new RegExp(pattern [, flags]);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-14
        • 2010-09-26
        • 1970-01-01
        • 2013-05-26
        • 1970-01-01
        • 2013-09-27
        • 2014-01-11
        • 1970-01-01
        相关资源
        最近更新 更多