5
/^(\-|\+)?([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.

Josh Crozier
  • 202,159
  • 50
  • 343
  • 273
Nick Graeff
  • 89
  • 1
  • 7
  • Also see: http://stackoverflow.com/questions/4736/learning-regular-expressions – jmunsch Nov 19 '15 at 01:20
  • http://regexper.com/#%2F%5E(%5C-%7C%5C%2B)%3F(%5B0-9%5D%2B%7CInfinity)%24%2F – epascarello Nov 19 '15 at 01:25
  • Lately I discovered [Regex101.com](https://regex101.com/r/kS1mL0/1) an Web 2.0 site to discover, debug and test regular expressions. It also has a single step debugger. – Paebbels Nov 19 '15 at 23:22
  • Possible duplicate of [Reference - What does this regex mean?](http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) – Mariano Nov 20 '15 at 01:41

2 Answers2

6

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"

David Zorychta
  • 11,768
  • 4
  • 38
  • 73
3

It's a regular expression that will match positive/negative natural numbers or 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-9]+|Infinity)$ - The end of the string should be 1 or more digits or the string 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)$/);
Josh Crozier
  • 202,159
  • 50
  • 343
  • 273