Home >Web Front-end >JS Tutorial >Example analysis of non-capturing grouping usage of JS regular expressions
The examples in this article describe the usage of non-capturing grouping of JS regular expressions. Share it with everyone for your reference, the details are as follows:
When I was looking at JsonSQL recently, I learned what non-capturing grouping is and its usage scenarios through a regular expression in the source code. In js, the normal capturing grouping format is (XX), and the non-capturing grouping format is (?:XX). Let's start with the regular expression quantifier. If we require the character b to appear at least once, we can use the regular /b+/; if we require ab to appear at least once, then we must use /(ab)+/, not /ab+/. In other words, if you want to use quantifiers for multiple characters, you must use parentheses.
var str = "a1***ab1cd2***c2"; var reg1 = /((ab)+\d+)((cd)+\d+)/i; var reg2 = /((?:ab)+\d+)((?:cd)+\d+)/i; alert(str.match(reg1));//ab1cd2,ab1,ab,cd2,cd alert(str.match(reg2));//ab1cd2,ab1,cd2
You can see the difference between capturing groups and non-capturing groups: non-capturing groups are only used for matching and will not extract the group content. In other words, if we only want to use parentheses to modify some characters with quantifiers, we do not need the content of this group, which is a non-capturing group.
The following code is used to extract various sub-fragments in the sql statement. It uses a large number of non-capturing groups, which can be savored carefully.
var returnfields = sql.match(/^\s*SELECT\s+((?:[0-9A-Za-z_]+\s*,\s*)+[0-9A-Za-z_]+ |\*|[0-9A-Za-z_]+)\s+FROM\s+([a-z0-9A-Z_]+)(?: where\s+(.+))?(?:\s+order\s+by\s+([a-z0-9_A-Z]+)(?:\s+(asc|desc|ascnum|descnum)?))?(?:\s+limit\s+(\d+,\d+))?/i); var ops = { fields: returnfields[1].replace('\s','').split(','), from: returnfields[2].replace('\s',''), where: (returnfields[3] == undefined)? "true":returnfields[3], orderby: (returnfields[4] == undefined)? []:returnfields[4].replace('\s','').split(','), order: (returnfields[5] == undefined)? "asc":returnfields[5], limit: (returnfields[6] == undefined)? []:returnfields[6].replace('\s','').split(',') };
There are several explanations about this regular rule:
1. Field names and indications can only be composed of uppercase and lowercase letters, numbers and Made up of underscores.
2. The condition after where must be placed in (), otherwise it cannot be matched. This is different from real SQL.
3. The fields after select have 3 formats: single field, multiple fields (separated by commas), and all fields (indicated by *).
4. The where sub-statement, order by sub-statement, and limit sub-statement are all optional.
The following text can match the above regular expression:
select age from data where (name=='aty')
I hope this article will be helpful to everyone in JavaScript programming.
For more related articles on non-capturing grouping usage examples of JS regular expressions, please pay attention to the PHP Chinese website!