正则表达式的数字介于0到100之间? [英] Regex for number between 0 to 100?

查看:662
本文介绍了正则表达式的数字介于0到100之间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它不应该允许前导零,例如 099 .允许的值应如下所示: 35 99 1 2 3 100 .

It should not allow leading zeroes, like 099. Allowed values should be like these: 35, 99, 1, 2, 3, 100.

这是我尝试过的:

$('#createCoupon_discountAmount').bind('input propertychange', function () {
    $(this).val($(this).val().replace(/(\d\d?|100)\Z/g, ''));
});  

推荐答案

我将为此使用的正则表达式如下,但是我不确定它是否是最佳的:

The regex that I would use for this is the following, but I'm not sure if it an optimal one:

^([0-9] |([1-9] [0-9])| 100)$

我们创建三个组,并且始终从字符串的开头到结尾进行匹配.我们捕获的第一组是 [0-9] ,以获得前10个数字(0-9).第二组我们两次捕获这些数字,以从10-> 99获得所有数字.最后我们也匹配100.

We create three groups and we always match from the beginning of the string to the end. The first group we capture is [0-9]to get the first 10 numbers (0-9). The second group we capture those numbers twice, to get all numbers from 10 -> 99. And finally we just match 100 as well.

这篇关于正则表达式的数字介于0到100之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆