正则表达式未终止的字符串常量错误Javascript

时间:2012-04-10 12:46:49

标签: jquery regex validation

我使用过这个正则表达式^(?=。 [a-zA-Z]。)[a-zA-Z0-9?。! @#'\ s] {2200} $。它应该包含特殊的(! @#),字母数字字符,它应该允许最多200个字符。 但在这里我得到的错误像未终止的字符串常量..在谷歌我试过...它说我们需要重新生成元字符,如$,*与\ 我不能这样做。任何人都可以帮助我

我的代码就像这样

$.validationEngineLanguage = {
    newLang: function(){
        $.validationEngineLanguage.allRules = {
            "required": { // Add your regex rules here, you can take telephone as an   example
                "regex": "none",
                "alertText": "This field is required",
                "alertTextCheckboxMultiple": "* Please select an option",
                "alertTextCheckboxe": "* This checkbox is required",
                "alertTextDateRange": "* Both date range fields are required"
            },
            ---
             ----
            "passwordval": {
            "regex":^(?=.*[a-zA-Z].*)[a-zA-Z0-9 ?.!*@#'\s]{2,200}$,
            "alertText": "* Not a valid password"
            },
            };

    }
};

$.validationEngineLanguage.newLang();

我在“passwordval”中遇到问题:

2 个答案:

答案 0 :(得分:0)

var reExample = new RegExp("^(?=.[a-zA-Z].)[a-zA-Z0-9 ?.!@#'\s]{2,200}$");

注意“”

reExample.test("dfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdfdfsfsdfsdf");

以上产生错误

reExample.test("dfsfsdfsdfdfsfsd");

这会产生真实的

(假设您使用的是javascript)

答案 1 :(得分:0)

您的Regexp中似乎​​有错误

  • 更改此行

    "regex" : ^(?=.*[a-zA-Z].*)[a-zA-Z0-9 ?.!*@#'\s]{2,200}$,
    

    "regex" : "^(?=.*[a-zA-Z].*)[a-zA-Z0-9 ?.!*@#'\\s]{2,200}$",
    

注意

The double quote around the regular expression and the extra \ before \s