正则表达式匹配分数 [英] RegExp to match fraction

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

问题描述

我正在尝试执行正则表达式来匹配分数.
用户将输入一个分数,例如 1/4、1 1/2 10/2 等.
我已经测试了这个正则表达式并且它有效,但问题是当我输入 10/2、20/5、30/3、40/53 等等时它无法识别这些分数.这是我的正则表达式
如您所见,它首先整理出整数,然后是斜线,最后是斜线后的整数.

I'm trying to perform regex to match a fraction.
The user will input a fraction eg., 1/4, 1 1/2 10/2 so on.
I have tested this regex and it works, but the problem is when I type in 10/2, 20/5, 30/3, 40/53 so on It does not recognized these fraction. This is my regex
As you can see, it first sorted out the integer and then the slash and lastly the integer after the slash.

   var check_zero_value = str1.match(/[1-9]\/[1-9]/g);

   if(!check_zero_value1)
                {
                    return false;
                }

对此有什么想法吗?

推荐答案

要验证整数和分数,您可以执行以下操作:

To validate both integers and fractions you can do:

/[1-9][0-9]*(?:\/[1-9][0-9])*/g

链接到 Fiddler

如果你想要分数:

/[1-9][0-9]*\/[1-9][0-9]*/g

链接到 Fiddler

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

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