The usage of parentheses () in regular expressions and the usage of parentheses
First, we have introduced that if the second parameter in the replace () method is a callback function, the parameters in this callback function are the results after successful matching ....., are there multiple parameters in the callback function considered?
Here is an example:
<!DOCTYPE>
In the above example, You need to view the values of a1, a2, and a3 respectively. You will find some rules.
Separately view a1: 2015-8-
View separately a2: 2015 8
View a3 separately :--
Let's take an example of the three parameter images. a1 is equivalent to mother, a2 is the eldest son, a3 is the youngest son, and each son inherits part of the mother's genes.
A1 is the result of var re =/\ d +-/g;
A2 is the result of var re =/(\ d +) (-)/g; the first bracket on the left
A2 is the result of var re =/(\ d +) (-)/g; the second bracket on the left
After talking about the usage of so many parameters, we can achieve the output below: 2015.8.7
Method 1: perform operations on the Son
<!DOCTYPE>
Method 2: operate on the mother
<!DOCTYPE>
I learned the relationship between multiple parameters in the callback function of the replace () method in the Regular Expression and the parentheses, and instantly felt that the regular expression was complicated ....
What is the difference between regular expressions [] and parentheses?
The most basic meaning is that parentheses are regarded as a whole, and the brackets match one of them, and the braces match several times.
However, adding other characters to the brackets may have different meanings. For example:
{N}
N is a non-negative integer. It matches the specified n times. For example, "o {2}" cannot match "o" in "Bob", but it can match two o in "food.
{N ,}
N is a non-negative integer. match at least n times. for example, "o {2,}" cannot match "o" in "Bob", but it can match all o in "foooood. "o {1,}" is equivalent to "o + ". "o {0,}" is equivalent to "o *".
{N, m}
Both m and n are non-negative integers, where n
[] Meaningless, generally used to match a set
For example
[0-9]: Find any number ranging from 0 to 9.
[A-Z]: searches for any characters from a to uppercase Z.
() Also matches a set, but it can also be used as a reverse reference.
The above is all the content of this article. I hope you will like it.