Grouping concept and usage examples of javascript regular expressions

  • 2021-06-28 10:58:53
  • OfStack

The grouping concept and usage of javascript regular expressions are illustrated with examples.Share it for your reference, as follows:


function matchDemo(){
  var s;
  // The expression is divided 3 Groups: d(b+)(d) , (b+) , (d) this 3 Group (actually 4 Groups, including all expressions of their own) 
  // From the leftmost number 1 Brackets are # 1 Groups, No. 2 Brackets are # 2 Group, and so on, where the values are RegExp.$1 and RegExp.$2 Value of 
  var re = new RegExp("(d(b+)(d))","ig");
  var str = "cdbBdbsbdbdz";
  //exec() The returned value was found by matching the expression 1 Arrays 
  var arr = re.exec(str);
  // accord with d(b+)(d) The value returned by this expression 
  s = "$1 contains: " + RegExp.$1 + ", RegExp.$1 : " + RegExp.$1.length + "\n";
  // accord with (b+) The value returned by this expression 
  s += "$2 contains: " + RegExp.$2 + ", RegExp.$2 : " + RegExp.$2 + "\n";
  // accord with (d) The value returned by this expression 
  s += "$3 contains: " + RegExp.$3 + ", RegExp.$3 : " + RegExp.$3;
  // Get the last matching string 1 The position of characters in the string, that is, the start of the next match 
  alert(RegExp.lastIndex);
  // If a value exists, the last one is returned 1 Grouped results ( Returns the last parenthesis sub-match of any regular expression lookup )
  alert(RegExp.lastParen);
  // Get the last matching string ( Returns the last matching character in any regular expression search )
  alert(RegExp.lastMatch);
  //leftContext + lastMatch + rightContext == context
  alert(RegExp.leftContext);
  alert(RegExp.rightContext);
  // The result is 2 Grouped results 
  alert(RegExp.$2);
  return(s);
}
alert(matchDemo());


function matchDemo2(){
  var s,temp;
  // The results are divided into two groups (b+) , (d) And, of course, all of them (d(b+)(d)) Is the default group 
  var re = new RegExp("d(b+)(d)","ig");
  var str = "cdbBdbsbdbdz";
  //arr Results include 3 Groups ( Include all modes ) Return Results 
  //dbBd , bB , d and dbd , b , d The two patterns match, while the 2 And 3 The number of arrays is actually in the 1 Match on the basis of (b+) , (d) Both modes 
  // That is, the grouping is matched in the fully matched mode, so as to " screen " Functions 
  while((arr = re.exec(str)) != null)
  {
      alert(arr);
      temp = "$1 contains: " + RegExp.$1 + ", RegExp.$1.length : " + RegExp.$1.length + ",RegExp.$1.lastIndex:" + RegExp.$1.lastIndex; 
      alert(temp);
      //$2 The attribute represents the matching number 2 Groups, that is (d) This mode 
      s = "$2 contains: " + RegExp.$2 + ", RegExp.$2.length : " + RegExp.$2.length;
      alert(s);
  }
}
matchDemo2();

More readers interested in JavaScript related content can view this site's topics: JavaScript Switching Special Effects and Techniques Summary, JavaScript Finding Algorithmic Techniques Summary, JavaScript Animation Special Effects and Techniques Summary, JavaScript Errors and Debugging Techniques Summary, JavaScript Data Structure and Algorithmic Techniques Summary,Summary of JavaScript Traversal Algorithms and Techniques and Summary of JavaScript Mathematical Operation Usage

I hope that the description in this paper will be helpful to everyone's JavaScript program design.


Related articles: