简体   繁体   English

如何使用javascript的计算器输入使用正则表达式?

[英]How to use regular expression for calculator input with javascript?

I am trying to write simple calculator with JavaScript. 我正在尝试用JavaScript编写简单的计算器。 I want to check if user input is correct or not. 我想检查用户输入是否正确。 I wrote regular expression in order to make sure user input is proper but it is not working. 我写了正则表达式,以确保用户输入正确,但它不起作用。 What I want to do is a simple rule which is operator(/*-+) should be followed by operand(0-9) but first operator can be - or +. 我想要做的是一个简单的规则,运算符(/ * - +)后面应该是操作数(0-9),但第一个运算符可以是 - 或+。 The regular expression below is not working. 下面的正则表达式不起作用。 What is my mistake? 我的错是什么?

^[-+]?[0-9]{0,}([-+*/]?)[0-9]+$

Your expression looks for an optional - or + followed by zero or more digits ( [0-9]{0,} ). 您的表达式查找可选-+后跟或更多数字( [0-9]{0,} )。 I think you probably wanted one or more ( + ). 我想你可能想要一个或多个( + )。 Similarly, the ? 同样, ? in ([-+*/]?) makes the operator optional . in ([-+*/]?)使操作符可选 You probably also want to capture the various parts. 您可能还想捕获各个部分。

It is fine when I have something one operator between two operand but when I do 9+9+9 which is more then two it is not working. 当我在两个操作数之间有一个操作符时,这很好,但是当我执行9 + 9 + 9时,它更多的是两个操作数。

If you want to allow additional operators and digits following them, you have to allow the latter part to repeat. 如果要允许其后的附加运算符和数字,则必须允许后一部分重复。

Here's my rough take: 这是我的粗略看法:

^\s*([-+]?)(\d+)(?:\s*([-+*\/])\s*((?:\s[-+])?\d+)\s*)+$

Live Example (But there's something not quite right happening with the capture groups if you have three or more terms.) 实例 (但如果您有三个或更多术语,那么捕获组会发生一些不太正确的事情。)

Changes I made: 我做的改变:

  1. Use \\d rather than [0-9] ( \\d stands for "digit") 使用\\d而不是[0-9]\\d代表“数字”)

  2. Put the digits and the operators in capture groups. 将数字和运算符放在捕获组中。

  3. Don't make the digits or operator optional. 不要使数字或运算符可选。

  4. Allow optional whitespace ( \\s* ) in various places. 在不同的地方允许可选的空格( \\s* )。

  5. The (?: _________ )+ is what lets the part within those parens repeat. (?: _________ )+是让那些parens中的部分重复的原因。

  6. Allow a - or + in front of the second group of digits, but only if preceded by a space after the operator. 在第二组数字前面允许-+ ,但前提是在运算符后面有空格。

^[-+]?

Is correct, but then 是正确的,但随后

[0-9]{0,}

Is not, you want + quantifier as if you use {0,} (which is the same as *) you could have "+*9" being correct. 不是,你想要+量词,好像你使用{0,}(与*相同),你可以让“+ * 9”正确。 Then, 然后,

([-+*/]?)[0-9]+

Is wrong, you want : 是错的,你想:

([-+*/]+[-+]?[0-9]+)*

So you will have for instance *-523+4234/-34 (you can use an operand on an either negative or positive number, or not precised number which would mean obviously positive) 所以你将拥有例如* -523 + 4234 / -34(你可以在一个负数或正数上使用一个操作数,或者不使用精确数字,这意味着明显是正数)

So finally, you would have something like : 最后,你会有类似的东西:

^[-+]?[0-9]+([-+*/]+[-+]?[0-9]+)*$

Of course, you can replace class [0-9] with \\d 当然,您可以用\\ d替换class [0-9]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM