简体   繁体   English

如何在javascript中使用正则表达式来匹配数字和符号?

[英]How do I use Regular Expressions in javascript to match numbers and symbols?

Say I have a string like the following: 说我有一个像下面这样的字符串:

var str = "23*45+ 4*12"

How can I use regex to return 我如何使用正则表达式返回

["23","*","45","+","4","*","12"]

How would you do it to include decimals and negative numbers as well? 您将如何同时包含小数和负数?

var str1 = "45.3*23+(-4*5.8)

Result should look like: 结果应如下所示:

["45.3","*","23","+","-4","*","5.8"]

Please explain your answer as I am new to regex. 由于我是正则表达式的新手,请解释您的答案。 Thanks! 谢谢!

The most trivial way to solve your particular problem is to split on word boundaries: 解决您的特定问题的最简单的方法是在单词边界上分割:

$ node
> "23*45+ 4*12".split(/\b/)
[ '23', '*', '45', '+ ', '4', '*', '12' ]

The regex /\\b/ is described here . 正则表达式/\\b/ 在此处描述。

Now since you have spaces, you should probably trim each result: 现在,由于您有空格,您可能应该修剪每个结果:

$ node
> "23*45+ 4*12".split(/\b/).map(function (s) {return s.trim()})
[ '23', '*', '45', '+', '4', '*', '12' ]

Now if you only want to capture the numbers and the arithmetic operators, you should probably match them directly rather than relying on splitting. 现在,如果您只想捕获数字和算术运算符,则可能应该直接匹配它们,而不是依赖于拆分。 The regex to match an integer is 匹配整数的正则表达式为

\d+

which is one or more digits, and the regex to match an operator is 是一个或多个数字,并且匹配运算符的正则表达式是

[=+/*]

Note that I put the dash first because if it were in the middle it would be a character range (like [az] which matches 26 characters whereas [-az] matches only three). 请注意,我将破折号放在第一位,因为如果它在中间,它将是一个字符范围(例如[az]匹配26个字符,而[-az]仅匹配三个字符)。 Now you can do this: 现在您可以执行以下操作:

$ node
> "23*45+ 4*12".match(/[-+/*]|\d+/g)
[ '23', '*', '45', '+', '4', '*', '12' ]

The g is a modifier to the regex that says get all the matches (the g stands for "global"). g是正则表达式的修饰符,表示获取所有匹配项( g代表“全局”)。 One interesting thing about the last approach is that it will skip characters that don't belong, so this can happen: 关于最后一种方法的一个有趣的事情是它将跳过不属于的字符,因此可能发生:

$ node
> "23  *  blah45 + 4*~~~~12".match(/[-+/*]|\d+/g)
[ '23', '*', '45', '+', '4', '*', '12' ]

Now let's suppose you wanted to add floating point numbers. 现在,假设您要添加浮点数。 Those look like 22.807 , ie, digits, then a dot, then more digits. 这些看起来像22.807 ,即数字,然后是点,然后是更多数字。 The dot is special in regexes, so we have to write it like this: \\. 该点在正则表达式中很特殊,因此我们必须这样写: \\. . So to capture something that can be either an integer or a floating point value we would write: 因此,为了捕获可以是整数或浮点值的内容,我们将编写:

\d+(\.d+)?

where the ? 在哪里? means optional. 表示可选。 Now we could also add a leading optional negative sign: 现在我们还可以添加一个前导的可选负号:

-?\d+(\.\d+)?

and as you know from programming, we like to write numbers like 255.84E-19 , which gets us to: 从编程中可以知道,我们喜欢编写数字255.84E-19 ,这使我们能够:

-?\d+(\.\d+([Ee][+-]?\d+)?)?

with an optional exponential part, containing either an upper or lower case E, then an optional sign, then a required exponent number. 带有可选的指数部分,包含大写或小写字母E,然后是可选的符号,然后是必需的指数编号。

Then there's this thing about parentheses having performance issues in "non-capturing" contexts (hard to explain here), so pros would write: 然后是关于括号在“非捕获”上下文中存在性能问题的问题(此处难以解释),因此专业人士会写:

-?\d+(?:\.\d+(?:[Ee][+-]?\d+)?)?

and in all its glory you get 在所有的荣耀中

$ node
> "23  *   -99.45 + 4.2E7 * -12".match(/-?\d+(?:\.\d+(?:[Ee][+-]?\d+)?)?|[-+*/]/g)
[ '23', '*', '-99.45', '+', '4.2E7', '*', '-12' ]

There's a lot going on here. 这里有很多事情。 Sorry to get so deep into it. 很抱歉深入了解它。

By the way, the general problem of parsing arithmetic expressions and validating them can't be done by a regex anyway, but hopefully this explanation will get you started. 顺便说一句,正则表达式无法解决解析算术表达式并对其进行验证的一般问题,但是希望这种解释可以帮助您入门。

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

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