This is a regular expression. The one you pasted would match a positive/negative whole number, or match the word 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. You can think of regular
expressions as wildcards on steroids.
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.
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.
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
$
means, "and then require that the string ends here"