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) (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 a array found in accordance with the expression var arr = re.exec(str); //The value returned by the expression according to d(b+)(d) = "$1 contains: " + RegExp.$1 + ", RegExp.$1 : " + RegExp.$1.length + "/n"; //The value returned by the expression in accordance with (b+) += "$2 contains: " + RegExp.$2 + ", RegExp.$2 : " + RegExp.$2 + "/n"; //The value returned by the expression in accordance with (d) += "$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 start position of the next match alert(RegExp.lastIndex); //If a value exists, the result of the last grouping is returned (returning the last submatch included in any regular expression search process) alert(RegExp.lastParen); //Get the last matching string (returning the last matching character in any regular expression search process) alert(RegExp.lastMatch); //leftContext + lastMatch + rightContext == context alert(RegExp.leftContext); alert(RegExp.rightContext); //The result is the result of the second group 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 group 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) //dbBd, bB, d and d, b, d, and the two patterns of dbBd, bB, d are matched on the first basis, and the second and third arrays are actually matched (b+) and (d) on the basis of the first.//That is, the grouping is matched in a fully matched pattern, which plays the function of "filtering" 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); //The $2 attribute represents matching the second group, that is, (d) this pattern s = "$2 contains: " + RegExp.$2 + ", RegExp.$2.length : " + RegExp.$2.length; alert(s); }}matchDemo2();For more information about JavaScript related content, please check out the topics of this site: "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 techniques", "Summary of JavaScript data structures and algorithm skills", "Summary of JavaScript traversal algorithms and techniques", and "Summary of JavaScript mathematical operations usage"
I hope this article will be helpful to everyone's JavaScript programming.