What I want is, there is a textbox with maximum length of 5. The values allowed are..
我想要的是,有一个最大长度为5的文本框。允许的值是..
- any integer // for example 1, 3, 9, 9239 all are valid
- real number, with exaclty one point after decimal // eg. 1.2, 93.7 valid and 61.37, 55.67 invalid
- it is also allowed to enter only decimal and a digit after that, that is .7 is valid entry (would be considered as 0.7)
任何整数//例如1,3,9,9239都是有效的
实数,小数点后的一点,例如。 1.2,93.7有效且61.37,55.67无效
也允许在此之后只输入十进制和一位数,即.7是有效输入(将被视为0.7)
I found this page, http://www.regular-expressions.info/refadv.html
So what I thought is that
我找到了这个页面,http://www.regular-expressions.info/refadv.html所以我的想法就是这样
- There is a digit
- If there is a digit and a decimal after that, there must be one number after that
- If there is no digit there must be a decimal and a digit after that
有一个数字
如果之后有一个数字和一个小数,那么之后必须有一个数字
如果没有数字,则必须有小数和数字
So, the regex I made is..
所以,我制作的正则表达式是......
a single digit one or more => /d+
an optional decimal point followed by exactly one digit => (?:[.]\d{1})?
if first condition matches => (?(first condition) => (?((?<=\d+)
then, match the option decimal and one exact digit =>(?((?<=\d+)(?:[.]\d{1})?
else => |
find if there is a decimal and one exact digit => (?:[.]\d{1}){1}
check the whole condition globally => /gm
overall expression =>
整体表达=>
(?(?<=\d+)(?:[.]\d{1}){1}|(?:[.]\d{1}){1})+/gm
But it doesn't outputs anything..
Here's the fiddle
但它没有输出任何东西..这是小提琴
ps: the pattern1 and pattern2 there, are related to my previous question.
ps:那里的pattern1和pattern2与我之前的问题有关。
4 个解决方案
#1
1
You can try the following pattern:
您可以尝试以下模式:
/^\d{0,4}\.?\d$/
It seems to fulfil all your requirements:
它似乎满足您的所有要求:
> /^\d{0,4}\.?\d$/.test(".4")
true
> /^\d{0,4}\.?\d$/.test(".45")
false
> /^\d{0,4}\.?\d$/.test("1234.4")
true
> /^\d{0,4}\.?\d$/.test("12345.4")
false
> /^\d{0,4}\.?\d$/.test("12345")
true
> /^\d{0,4}\.?\d$/.test("123456")
false
This pattern assumes that the number can have a maximum of five digits and an optional decimal point.
此模式假定该数字最多可包含五位数和一个可选的小数点。
If the maximum length of five includes the optional decimal point then the pattern is slightly more complex:
如果最大长度为5包含可选的小数点,则模式稍微复杂一些:
/^(?:\d{1,5}|\d{0,3}\.\d)$/
The first part of the group deals with integer numbers of the required length, the second option of the group deals with real numbers which maximum length (including the decimal point) is five.
该组的第一部分处理所需长度的整数,该组的第二个选项处理最大长度(包括小数点)为5的实数。
#2
2
Maybe you are complicating things too much. I did a quick test and unless I'm missing something this regex seems to work fine:
也许你太复杂了。我做了一个快速测试,除非我遗漏了这个正则表达式似乎工作正常:
/^\d*\.?\d$/
Demo: http://jsbin.com/esihex/4/edit
Edit: To check the length you can do it without regex:
编辑:要检查长度,您可以在没有正则表达式的情况下执行此操作:
if ( value.replace('.','').length <= 5 && regex.test( value ) ) {
...
}
Notice that I used replace
to remove the dots so they don't count as characters when getting the length.
请注意,我使用replace来删除点,这样在获取长度时它们不会被计为字符。
#3
1
Consider this code:
考虑以下代码:
var checkedString = "45.3 fsd fsd fsdfsd 673.24 fsd63.2ds 32.2 ds 32 ds 44 fasd 432 235f d653 dsdfs";
checkedString = " "+checkedString;
var results = checkedString.match(/[\s]{1}(\d+\.*\d{1})(?![\d\.\w])+/gm);
results.map(function(result) {
return result.trim();
});
Couldn't make it in other way because in JS (?<=
(lookbehind) regexp is not working.
无法以其他方式使用它,因为在JS(?<=(lookbehind)regexp不起作用。
This will be returned:
这将返回:
["45.3","32.2","32","44","432"]
So probably it's what you've expected.
所以可能是你所期望的。
#4
0
I don't know what are you trying to do with those conditionals in your regex. I also looked at your jsfiddle, which outputs nothing for me. But I made a two versions of a regex that matches the correct values for the textbox, which are ^(?!(.{6,}))(?:[1-9]\d*)*(?:\.\d*[1-9])?$
and ^(?!(.{6,}))(?:\d*)*(?:\.\d*)?$
.
我不知道你正在尝试用正则表达式中的那些条件做什么。我也看了你的jsfiddle,它没有为我输出任何内容。但我制作了一个正则表达式的两个版本,它匹配文本框的正确值,即^(?!(。{6,}))(?:[1-9] \ d *)*(?:\。 \ d * [1-9])?$和^(?!(。{6,}))(?:\ d *)*(?:\。\ d *)?$。
The first disallows to start with zero, or end with zero after the decimal.
第一个不允许从零开始,或在小数点后以零结束。
Comment if you need explanation of the regex.
如果您需要正则表达式的解释,请注释。
#1
1
You can try the following pattern:
您可以尝试以下模式:
/^\d{0,4}\.?\d$/
It seems to fulfil all your requirements:
它似乎满足您的所有要求:
> /^\d{0,4}\.?\d$/.test(".4")
true
> /^\d{0,4}\.?\d$/.test(".45")
false
> /^\d{0,4}\.?\d$/.test("1234.4")
true
> /^\d{0,4}\.?\d$/.test("12345.4")
false
> /^\d{0,4}\.?\d$/.test("12345")
true
> /^\d{0,4}\.?\d$/.test("123456")
false
This pattern assumes that the number can have a maximum of five digits and an optional decimal point.
此模式假定该数字最多可包含五位数和一个可选的小数点。
If the maximum length of five includes the optional decimal point then the pattern is slightly more complex:
如果最大长度为5包含可选的小数点,则模式稍微复杂一些:
/^(?:\d{1,5}|\d{0,3}\.\d)$/
The first part of the group deals with integer numbers of the required length, the second option of the group deals with real numbers which maximum length (including the decimal point) is five.
该组的第一部分处理所需长度的整数,该组的第二个选项处理最大长度(包括小数点)为5的实数。
#2
2
Maybe you are complicating things too much. I did a quick test and unless I'm missing something this regex seems to work fine:
也许你太复杂了。我做了一个快速测试,除非我遗漏了这个正则表达式似乎工作正常:
/^\d*\.?\d$/
Demo: http://jsbin.com/esihex/4/edit
Edit: To check the length you can do it without regex:
编辑:要检查长度,您可以在没有正则表达式的情况下执行此操作:
if ( value.replace('.','').length <= 5 && regex.test( value ) ) {
...
}
Notice that I used replace
to remove the dots so they don't count as characters when getting the length.
请注意,我使用replace来删除点,这样在获取长度时它们不会被计为字符。
#3
1
Consider this code:
考虑以下代码:
var checkedString = "45.3 fsd fsd fsdfsd 673.24 fsd63.2ds 32.2 ds 32 ds 44 fasd 432 235f d653 dsdfs";
checkedString = " "+checkedString;
var results = checkedString.match(/[\s]{1}(\d+\.*\d{1})(?![\d\.\w])+/gm);
results.map(function(result) {
return result.trim();
});
Couldn't make it in other way because in JS (?<=
(lookbehind) regexp is not working.
无法以其他方式使用它,因为在JS(?<=(lookbehind)regexp不起作用。
This will be returned:
这将返回:
["45.3","32.2","32","44","432"]
So probably it's what you've expected.
所以可能是你所期望的。
#4
0
I don't know what are you trying to do with those conditionals in your regex. I also looked at your jsfiddle, which outputs nothing for me. But I made a two versions of a regex that matches the correct values for the textbox, which are ^(?!(.{6,}))(?:[1-9]\d*)*(?:\.\d*[1-9])?$
and ^(?!(.{6,}))(?:\d*)*(?:\.\d*)?$
.
我不知道你正在尝试用正则表达式中的那些条件做什么。我也看了你的jsfiddle,它没有为我输出任何内容。但我制作了一个正则表达式的两个版本,它匹配文本框的正确值,即^(?!(。{6,}))(?:[1-9] \ d *)*(?:\。 \ d * [1-9])?$和^(?!(。{6,}))(?:\ d *)*(?:\。\ d *)?$。
The first disallows to start with zero, or end with zero after the decimal.
第一个不允许从零开始,或在小数点后以零结束。
Comment if you need explanation of the regex.
如果您需要正则表达式的解释,请注释。