Javascript的正则表达式:验证双/浮动 [英] Javascript Regex: validating a double/float

查看:179
本文介绍了Javascript的正则表达式:验证双/浮动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var re = /(\ d {1,2} \\ \\(= \d {1,2}?))/。 

我想要返回的是一个或两个数字的数字(永远不会更大比24,因为它是一个时间mgmt的应用程序),可能或不可以跟随一个小数点与一个或两个尾随数字,但不能超过两个。



我不确定括号内的子字符串是否与前瞻混合在一起。我只是猜测和嵌套他们。最终,如果我的语法没问题,我认为我唯一缺少的是如何建议该模式可能有也可能没有前导数字,可能包含也可能不包含一个十进制的数字。



让我知道你是否需要更多信息。



更新,例子:

我们只处理时间,没有更多的时间在一天之内发生。 24元将是最高的输入。



有效:

  23.75 
1.4
1
0.5
0
.2

无效:

  1.897 
%#$#@ $#
Words
其他字符

最新更新:

由于这是一个小数,23.75作品。我们不计算分钟数,而是计算分钟数。

另外,为了记录,我尝试使用方法和条件进行验证,并且让字母在小数点。我决定用正则表达式来解决问题。解决方案

如果给定的数字小于24,那么不需要单独测试,那么下面的表达式将起作用。

  ^ \ d {0,2}(\.\d {0,2}){0,1 } 

请参阅 http://rubular.com/r/YDfHr5T5sQ



测试对象:

  23.75 pass 
1.4 pass
1 pass
0.5 pass
0 pass
.2 pass
1.897 fail
%#$#@ $#失败
单词失败
其他字符失败

说明:

  ^匹配字符串开头
\d {0,2}寻找零到两位数
(){0,1} $查找下一个零或一次,那么字符串
\\d {0,2}的末尾恰好匹配一位小数其次是最多两位

注意 - 这个正则表达式匹配空字符串。你可能想单独测试一下,如果有机会以某种方式来表达这个表达式的话...



简单的代码在javascript中测试:

  var str =12.345; 
var m = str.match(/ ^ \ d {0,2}(?: \.\d {0,2}){0,1} $ /);
var goodTime;
if(!m){
alert(str +不是一个好时机);
}
else {
goodTime = m [0];
alert(找到好时间+ goodTime);

$ / code>

注意 - 我调整了 regex - 在小数位后组中添加?:。这只是意味着匹配但不捕获,所以结果将只返回匹配 m [0] 而不是组 .34 in m [1] 。这实际上并不重要,因为我把 goodTime 的值赋给m [0](但是只有当它是一个好时机的时候)。您可以在这里看到这个


Here's the pattern I'm working on:

var re = /(\d{1,2}\.(?=\d{1,2}))/;

What I would like for this to return is a one or two digit number (which will never be greater than 24, since it is for a time mgmt app), which may or may not be followed by a decimal point with either one or two trailing digits, but not more than two.

I'm not sure about the parenthetical substring match mixed with the lookahead. I just guessed and nested them. Ultimately, if my syntax is okay, I think the only thing I am missing is how to suggest that the pattern may or may not have leading digits, and may or may not contain a decimal with trialing digits.

Let me know if you need more info.

Update, Examples:

We are only dealing with time, and no more time than can occur in a single day. 24 would be the highest input.

Valid:

23.75
1.4 
1
0.5 
0
.2

Invalid:

1.897
%#$#@$#
Words
other characters

Newest Update:

Since this is a decimal, 23.75 works. We are not counting minutes, but rather fractions of hours.

Also, for the record, I tried validating using methods and conditionals, and it was letting letters pass through after the decimals. I have made the decision to go with regex.

解决方案

If "any number given will be less than 24", so that doesn't need to be separately tested for, then the following expression will work.

^\d{0,2}(\.\d{0,2}){0,1}$

See http://rubular.com/r/YDfHr5T5sQ

Tested against:

23.75             pass
1.4               pass
1                 pass
0.5               pass
0                 pass
.2                pass
1.897             fail
%#$#@$#           fail
Words             fail
other characters  fail

Explanation:

^             start matching at the start of the string
\d{0,2}       look for zero to two digits
(   ){0,1}$   look for this next thing zero or one time, then the end of the string
\.\d{0,2}     match exactly one decimal followed by up to two digits

Note - this regex does match the "empty string". You might want to test for that separately if there's a chance that will somehow make its way to this expression...

Simple code to test in your javascript:

var str = "12.345";
var m = str.match(/^\d{0,2}(?:\.\d{0,2}){0,1}$/);
var goodTime;
if (!m) {
    alert(str + " is not a good time");
}
else {
    goodTime = m[0];
    alert("found a good time: " + goodTime);
}

Note - I made a tweak to the regex - adding ?: in the "bit after the decimal" group. This just means "match but don't capture" so the result will return only the match m[0] and not the group .34 in m[1]. It doesn't actually matter since I assign goodTime the value in m[0] (but only if it's a good time). You can see this in action here

这篇关于Javascript的正则表达式:验证双/浮动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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