[英]Can someone explain what /^(\-|\+)?([0-9]+|Infinity)$/ is?
/^(\-|\+)?([0-9]+|Infinity)$/
I've seen this multiple times when I'm looking to filter things. 当我想要过滤事物时,我已经多次看到这种情况。 There are many variations but it usually always starts with (/ then something. Recently I found this as a suggestion to help parse a string and make sure it has only numbers in it. On Mozilla's js page for RegExp I found some other operators but it didn't include nearly all that are above.
有很多变体,但通常总是以(/开头。最近,我发现这是一个建议,以帮助解析字符串并确保其中仅包含数字。在Regexp的Mozilla js页面上,我发现了其他一些运算符,但是几乎不包含上述所有内容。
This is a regular expression. 这是一个正则表达式。 The one you pasted would match a positive/negative whole number, or match the word infinity.
您粘贴的数字将匹配正/负整数,或者匹配单词infinity。 In short, a regular expression is:
简而言之,正则表达式为:
A regular expression (regex or regexp for short) is a special text string for describing a search pattern.
正则表达式(简称regex或regexp)是用于描述搜索模式的特殊文本字符串。 You can think of regular expressions as wildcards on steroids.
您可以将正则表达式视为类固醇上的通配符。
http://www.regular-expressions.info/ http://www.regular-expressions.info/
You often see regular expressions written as /expression_here/
because these slashes in many programming languages are a shorthand way for developers to build regular expression objects. 您经常会看到正则表达式写为
/expression_here/
因为在许多编程语言中,这些斜杠是开发人员构建正则表达式对象的捷径。
You could create a simple expression to match a number with something like: 您可以创建一个简单的表达式来将数字与以下内容匹配:
/^[0-9]*$/.test('44') // returns true
and 和
/^[0-9]*$/.test('asdasd') // returns false
Expressions like these and like the one you've pasted are parsed and turned into little machines (called finite state machines ). 像这样的表达式以及像您粘贴的那样的表达式将被解析并变成小型机器(称为有限状态机 )。 the entire purpose of the machine is to determine if a string is a match for the expression the machine represents, or if it is not a match.
机器的整个目的是确定字符串是否与机器表示的表达式匹配,或者是否与字符串匹配。 You can then feed a string into such a machine and it will spit back the answer to you.
然后,您可以将字符串输入到这样的机器中,它将向您吐出答案。
In our example above, we feed the strings 44
and asdasd
into a regular expression /^[0-9]*$/
using the test
method, and it returns true
because 44
matches the expression and false
for asdasd
because it does not match. 在上面的示例中,我们使用
test
方法将字符串44
和asdasd
馈入到正则表达式/^[0-9]*$/
,由于44
与表达式匹配,因此返回true
因为asdasd
与表达式不匹配,因此返回false
。
We can break up the regular expression that you included in your post as well: 我们还可以分解您包含在帖子中的正则表达式:
^
means that the regular expression has to match starting from the VERY beginning of the string ^
表示正则表达式必须从字符串的非常开头开始匹配
(\\-|\\+)
means start at the beginning of the string and match either -
or +
, the question mark means this part is optional (\\-|\\+)
表示从字符串的开头开始,并且匹配-
或+
,问号表示此部分是可选的
[0-9]+|Infinity
means "match one or more numbers from 0 to 9", OR ( |
) match the text Infinity
[0-9]+|Infinity
表示“匹配一个或多个0到9之间的数字”,或( |
)匹配文本Infinity
$
means, "and then require that the string ends here" $
表示“然后要求字符串在此处结束”
It's a regular expression that will match positive/negative natural numbers or Infinity
. 这是一个匹配正/负自然数或
Infinity
正则表达式 。
/^(\-|\+)?([0-9]+|Infinity)$/
^(\\-|\\+)
- Match the beginning of the string for either the -
or +
literal character(s). ^(\\-|\\+)
-将字符串的开头与-
或+
文字字符匹配。
?
- The preceding expression, which are the -
/ +
characters, are optional. -前面的表达式是
-
/ +
字符,是可选的。 In other words, the expression can be matched 0 or 1 times. 换句话说,表达式可以匹配0或1次。
([0-9]+|Infinity)$
- The end of the string should be 1 or more digits or the string Infinity
. ([0-9]+|Infinity)$
-字符串的末尾应为1个或多个数字或字符串Infinity
。
// Matches:
'-100'.match(/^(\-|\+)?([0-9]+|Infinity)$/);
'+100'.match(/^(\-|\+)?([0-9]+|Infinity)$/);
'Infinity'.match(/^(\-|\+)?([0-9]+|Infinity)$/);
// Does NOT match:
'5%'.match(/^(\-|\+)?([0-9]+|Infinity)$/);
'20/1'.match(/^(\-|\+)?([0-9]+|Infinity)$/);
'NaN'.match(/^(\-|\+)?([0-9]+|Infinity)$/);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.