SoFunction
Updated on 2025-03-09

Detailed explanation of the purpose of parentheses in regular rules

First of all, we have introduced earlier that if the second parameter in the replace() method is a callback function, then the parameters in this callback function are the result after the match is successful. . . . . , So have you considered that there are multiple parameters in the callback function?

Let me give you an example first:

<!DOCTYPE>
<html>
<head>
  <meta charset='utf-8'> 
  <title></title>
</head>
<script type="text/javascript">
=function(){
  var date='2015-8-7';
  var re=/(\d+)(-)/g;
  (re,function(a1,a2,a3){
    /*alert(a1);*/
    alert(a2);
    /*alert(a3);*/
  });
};
</script>
<body>
</body>
</html>

The above example requires you to check the values ​​of a1, a2, and a3 respectively, and you will find some rules.

View a1 separately: 2015- 8-
View a2 separately: 2015 8
View a3 separately:--

Let’s give an example of the three parameters and images. a1 is equivalent to the mother, a2 is the eldest son, and a3 is the younger son. 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;

Having talked about the uses of parameters, let’s implement the output: 2015.8.7

Method 1: Operation on your son

<!DOCTYPE>
<html>
<head>
  <meta charset='utf-8'> 
  <title></title>
</head>
<script type="text/javascript">
=function(){
  var date='2015-8-7';
  var re=/(\d+)(-)/g;
  var a=(re,function(a1,a2,a3){
    return a2+'.';
  });
  alert(a);
};
</script>
<body>
</body>
</html>

Method 2: Operation for your mother

<!DOCTYPE>
<html>
<head>
  <meta charset='utf-8'> 
  <title></title>
</head>
<script type="text/javascript">
=function(){
  var date='2015-8-7';
  var re=/(\d+)(-)/g;
  var a=(re,function(a1,a2,a3){
    return (0,-1)+'.';
  });
  alert(a);
};
</script>
<body>
</body>
</html>

After learning the relationship between multiple parameters and brackets of the callback function of the replace() method in the regularity, I instantly felt that the regularity became complicated. . . .

What is the difference between square brackets[] and round brackets() in regular expressions?

The most basic meaning: brackets are regarded as a whole in brackets, brackets are to match one of the brackets, and braces are to match several times
But adding other characters to brackets has different meanings. For example:
{n}
n is a non-negative integer. Matches the n times determined. For example, "o{2}" cannot match "o" in "Bob", but can match two os in "food".
{n,}
n is a non-negative integer. Match at least n times. For example, "o{2,}" cannot match "o" in "Bob", but can match all os in "fooooood". "o{1,}" is equivalent to "o+". "o{0,}" is equivalent to "o*".
{n,m}
m and n are non-negative integers, where n

[] meaningless, generally used to match a set
for example
[0-9] : Find any number from 0 to 9.
[a-Z] : Find any character from lowercase a to uppercase Z.
() also matches a collection, but it can also be used as a backreference.

The above is the entire content of this article, I hope you like it.