Can someone explain that / ^ (\ - | \ +)? ([0-9] + | Infinity) $ / is?

/^(\-|\+)?([0-9]+|Infinity)$/ 

I have seen this several times when I try to filter things. There are many variations, but usually it always starts with (or something.) I recently found this as a suggestion to help parse a string and make sure that it has only numbers. On the Mozilla js page for RegExp, I found several other operators, but this did not include almost everything above.

+5
source share
2 answers

This is a regular expression. The one you inserted will correspond to a positive / negative integer or correspond to the infinity of the word. In short, regex:

A regular expression (regular expression or regular expression for short) is a special string text to describe 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 an abbreviated way for developers to create regular expression objects.

You can 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 those similar to the one you inserted are processed and converted into small machines (called machines with final state ). the whole purpose of the machine is to determine if the string matches the expression represented by the machine, or if it does not match. Then you can pass the string to such a machine, and it will return the answer to you.

In our example above, we root lines 44 and asdasd in the regular expression /^[0-9]*$/ using the test method and returns true because 44 matches the expression and false for asdasd because it does not match.

We can also break up the regex that you included in your post:

^ means regular expression must match starting at VERY beginning of line

(\-|\+) means the beginning at the beginning of the line and matching either - or + , a question mark means that this part is optional

[0-9]+|Infinity means "match one or more numbers from 0 to 9", OR ( | ) matches the text of Infinity

$ means "and then requires the line to end here"

+6
source

This is a regular expression that will match positive / negative positive integers or Infinity .

 /^(\-|\+)?([0-9]+|Infinity)$/ 

^(\-|\+) - match the beginning of a line for a character - or + literal.

? - The previous expression, which is the characters - / + , is optional. In other words, an expression can be matched 0 or 1 times.

([0-9]+|Infinity)$ - The end of the line must be 1 or more digits or the Infinity line.

 // 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)$/); 
+3
source

Source: https://habr.com/ru/post/1236279/


All Articles