Javascript 中动态 RegExp 构造的问题

发布于 2024-09-24 16:09:42 字数 706 浏览 4 评论 0原文

此方法是为了防止用户输入除数字和“允许的字符”之外的任何内容。允许的字符作为参数 allowedchars 传递。

到目前为止,该方法阻止数字输入,但允许的字符不起作用(尝试传递“-”(连字符)和“.”(句点))。所以我假设我的动态正则表达式构造不正确。帮助?

提前致谢!

numValidate : function (evt, allowedchars) {
    var theEvent, key, regex,
    addToRegex = allowedchars;

    theEvent = evt || window.event;
    key = theEvent.keyCode || theEvent.which;
        key = String.fromCharCode(key);
        var regex = new RegExp('/^[0-9' + addToRegex + ']$/');
        if (!regex.test(key)) {
            theEvent.returnValue = false;
            if (theEvent.preventDefault) {
                theEvent.preventDefault();
            }
        }
}

(ps. jQuery 解决方案也很好)

This method is to prevent users from entering anything but numbers and "allowed characters." Allowed characters are passed as the parameter allowedchars.

So far, the method prevents number entries but the allowedchars doesn't work (tried with passing "-" (hyphen) and "." (period)). So I'm assuming my dynamic regex construction isn't correct. Help?

Thanks in advance!

numValidate : function (evt, allowedchars) {
    var theEvent, key, regex,
    addToRegex = allowedchars;

    theEvent = evt || window.event;
    key = theEvent.keyCode || theEvent.which;
        key = String.fromCharCode(key);
        var regex = new RegExp('/^[0-9' + addToRegex + ']$/');
        if (!regex.test(key)) {
            theEvent.returnValue = false;
            if (theEvent.preventDefault) {
                theEvent.preventDefault();
            }
        }
}

(ps. jQuery solutions are fine too)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哀由 2024-10-01 16:09:42

1. 当您通过 new RegExp 构造时,无需包含周围的 /

    var regex = new RegExp('^[0-9' + addToRegex + ']

2. 但是,如果 addToRegex 包含 ]-,则生成的正则表达式可能会变得无效或匹配过多。因此,您需要转义它们:

    var regex = new RegExp('^[0-9' + addToRegex.replace(/([\-\]])/g, '\\$1') + ']

3.,但是由于您无论如何都要检查 1 个字符,因此避免正则表达式可能会更容易。

    var pass = ("0123456789" + addToRegex).indexOf(key);
    if (pass == -1) {
      ...
);

2. 但是,如果 addToRegex 包含 ]-,则生成的正则表达式可能会变得无效或匹配过多。因此,您需要转义它们:


3.,但是由于您无论如何都要检查 1 个字符,因此避免正则表达式可能会更容易。


);

3.,但是由于您无论如何都要检查 1 个字符,因此避免正则表达式可能会更容易。

);

2. 但是,如果 addToRegex 包含 ]-,则生成的正则表达式可能会变得无效或匹配过多。因此,您需要转义它们:

3.,但是由于您无论如何都要检查 1 个字符,因此避免正则表达式可能会更容易。

1. When you construct via new RegExp, there's no need to include the surrounding /s.

    var regex = new RegExp('^[0-9' + addToRegex + ']

2. But if addToRegex contains ] or -, the resulting regex may become invalid or match too much. So you need to escape them:

    var regex = new RegExp('^[0-9' + addToRegex.replace(/([\-\]])/g, '\\$1') + ']

3. But since you are checking against 1 character anyway, it may be easier to avoid regex.

    var pass = ("0123456789" + addToRegex).indexOf(key);
    if (pass == -1) {
      ...
);

2. But if addToRegex contains ] or -, the resulting regex may become invalid or match too much. So you need to escape them:


3. But since you are checking against 1 character anyway, it may be easier to avoid regex.


);

3. But since you are checking against 1 character anyway, it may be easier to avoid regex.

);

2. But if addToRegex contains ] or -, the resulting regex may become invalid or match too much. So you need to escape them:

3. But since you are checking against 1 character anyway, it may be easier to avoid regex.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文