SoFunction
Updated on 2025-04-10

Example of grouping concept and usage of javascript regular expressions

This article describes the grouping concept and usage of javascript regular expressions. Share it for your reference, as follows:

function matchDemo(){
  var s;
  //The expression is divided into three groups: d(b+)(d), (b+), (d) these three groups (actually four groups, including all the expressions themselves)  //The first bracket from the leftmost number is the first group, the second bracket is the second group, and so on, the corresponding values ​​are the values ​​of RegExp.$1 and RegExp.$2 respectively.  var re = new RegExp("(d(b+)(d))","ig");
  var str = "cdbBdbsbdbdz";
  //The value returned by exec() is an array that matches the expression search  var arr = (str);
  //The value returned by the expression d(b+)(d) is in line with the value returned by  s = "$1 contains: " + RegExp.$1 + ", RegExp.$1 : " + RegExp.$ + "\n";
  //The value returned by the expression (b+) is in line with the value returned by  s += "$2 contains: " + RegExp.$2 + ", RegExp.$2 : " + RegExp.$2 + "\n";
  //The value returned by this expression is in line with (d)  s += "$3 contains: " + RegExp.$3 + ", RegExp.$3 : " + RegExp.$3;
  //Get the position of the last character of the matching string in the string, that is, the starting position of the next match  alert();
  //If a value exists, the result of the last grouping is returned (returns the last submatch included in any regular expression search process)  alert();
  //Get the last matching string (returns the last matching character in any regular expression search process)  alert();
  //leftContext + lastMatch + rightContext == context
  alert();
  alert();
  //The result is the result of the second grouping  alert(RegExp.$2);
  return(s);
}
alert(matchDemo());

function matchDemo2(){
  var s,temp;
  //The result is divided into two groups (b+) and (d), and of course, it also includes all (d(b+)(d)) as the default groups.  var re = new RegExp("d(b+)(d)","ig");
  var str = "cdbBdbsbdbdz";
  //The result of arr includes the results returned by three groups (including all patterns).  //The two patterns of dbBd, bB, d and d match, while the second and third arrays actually match the two patterns (b+) and (d) based on the first one.  //In other words, the grouping is matched in a fully matched pattern, which plays the function of "filtering"  while((arr = (str)) != null)
  {
      alert(arr);
      temp = "$1 contains: " + RegExp.$1 + ", RegExp.$ : " + RegExp.$ + ",RegExp.$:" + RegExp.$; 
      alert(temp);
      //The $2 attribute means matching the second group, that is, (d) this pattern      s = "$2 contains: " + RegExp.$2 + ", RegExp.$ : " + RegExp.$;
      alert(s);
  }
}
matchDemo2();

For more information about JavaScript, readers who are interested in reading this site's special topic:Summary of JavaScript switching effects and techniques》、《Summary of JavaScript search algorithm skills》、《Summary of JavaScript animation effects and techniques》、《Summary of JavaScript Errors and Debugging Skills》、《Summary of JavaScript data structure and algorithm techniques》、《Summary of JavaScript traversal algorithm and skills"and"Summary of JavaScript mathematical operations usage

I hope this article will be helpful to everyone's JavaScript programming.