This article mainly introduces the usage of non-capturing grouping in JS regular expressions, and analyzes in detail the concepts, functions, usage methods, and precautions of non-capturing grouping in regular expressions based on the instance form, for more information about JS regular expressions, see the examples in this article. We will share this with you for your reference. The details are as follows:
Recently, when reading JsonSQL, I learned about non-capturing groups and their application scenarios through a regular expression in the source code. In js, the normal capture group format is (XX), and the non-capture group format is (? : XX ). Let's start with the regular expression quantizer. If we want character B to appear at least once, we can use regular expression/B ++/. If we want AB to appear at least once, we must use/(AB) +/, cannot use/AB + /. That is to say, to use quantifiers for multiple characters, parentheses must be used.
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,cdalert(str.match(reg2));//ab1cd2,ab1,cd2
We can see the difference between a capture group and a non-capture group: A non-capture group is only used for matching and does not extract the group content. That is to say, if we only want to use parentheses to modify some characters with quantifiers, we do not need the content of this group. This is a non-capturing group.
The following code is used to extract each sub-segment in an SQL statement. A large number of non-capturing groups are used for better understanding.
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 for this regular expression:
1. The field Name and description can only contain uppercase/lowercase letters, numbers, and underscores.
2. The condition after where must be placed in (); otherwise, it cannot be matched. This is different from the real SQL statement.
3. fields after select are in the following format: single field, multiple fields (separated by commas), and all fields (represented ).
4. where substatements, order by substatements, and limit substatements are optional.
The text below matches the above regular expression:
select age from data where (name=='aty')
I hope this article will help you design JavaScript programs.
For more articles on examples of non-capturing group usage of JS regular expressions, refer to PHP Chinese website!