Basic Syntax of JavaScript Regular Expressions (recommended) and basic syntax of Regular Expressions
Related reading:
Basic Syntax of js Regular Expressions (essence)
Regular expression syntax
A regular expression is a text mode consisting of common characters (such as characters a to z) and special characters (such as metacharacters. This mode describes one or more strings to be matched when searching the text subject. A regular expression is used as a template to match a character pattern with the searched string.
Definition
JavaScript Regular Expressions can be defined in two ways, defining a string that matches a string similar to <% XXX %>.
1. Constructor
Copy codeThe Code is as follows:
Var reg = new RegExp ('<% [^ %>] + %>', 'G ');
2. Literal
Copy codeThe Code is as follows:
Var reg =/<% [^ %>] %>/g;
G: global, full-text search. By default, the first result is searched to stop I: ingore case, case-insensitive, and case-sensitive m: multiple lines by default, multi-line search (change the meaning of ^ and $ so that they match the beginning and end of the line on any line, not just at the beginning and end of the entire string)
Metacharacters
One important reason that regular expressions are prohibitive is that they have too many escape characters and many combinations,Metacharacters(Special characters with special meanings in regular expressions can be used to specify their leading characters.) not many
Metacharacters: [{^ $ | )? * +.
Not all metacharacters have specific meanings. In different combinations, metacharacters have different meanings. Let's take a look at the classification.
Character class
Generally, a regular expression is a character (one escape character) corresponding to a character string. The expression AB \ t indicates
However, we can use metacharacters [] to construct a simple class. The so-called class refers to an object that conforms to certain features. It is a generic object, not a specific character, we can use the expression [abc] to classify characters a, B, or c as one type. expressions can match such characters.
Metacharacters [] can be combined to create a class. We can also use metacharacters ^ to create reverse/negative classes. Reverse classes mean that they do not belong to the XXX class, the expression [^ abc] indicates content that is not a, B, or c.
Range
According to the above description, if we want to match a single number, the expression is like this.
[0123456789]
If it is a letter, then ..., the regular expression also provides a range class. We can use x-y to connect two characters to represent any character from x to y. This is a closed interval, that is, it contains x and ybenshen, it is easy to match lowercase letters.
[A-z]
What if I want to match all the letters? The classes in the [] structure can be connected. We can also write [a-zA-Z] in this way.
Predefine class
We just created several classes using regular expressions to represent numbers, letters, and so on, but this is also very troublesome to write. Regular Expressions provide us with several common predefined classes to match common characters.
With these predefined classes, It is very convenient to write some regular expressions. For example, if we want to match a string of AB + numbers + any characters, we can write AB \ d.
Boundary
Regular Expressions also provide several common boundary matching characters.
Check out an irresponsible email RegEx match (do not imitate it, as described in parentheses) \ w + @ \ w + \. (com) $
Quantifiers
The methods we introduced previously are all one-to-one matching. If we want to match a string with 20 numbers consecutively, do we need to write it like this?
\ D...
Therefore, some quantifiers are introduced in the regular expression.
Let's look at several examples of using quantifiers.
\ W + \ B Byron match word + boundary + Byron
Copy codeThe Code is as follows:
(/\ W + \ B Byron /). test ('Hi Byron '); // true (/\ w + \ B Byron /). test ('Welcome Byron '); // true (/\ w + \ B Byron /). test ('hibyron '); // false
\ D + \. \ d {1, 3} matches three decimal digits
Greedy mode and non-Greedy Mode
After reading the quantifiers mentioned above, some questions about the matching principle may be raised by colleagues who love to think about it. For example, if the quantifiers {3, 5} appear ten times in a sentence, in this case, three or five matches are matched at a time. The values 3, 4, and 5 meet the requirements of three to five matches ~ 5. By default, quantifiers are matched as many as possible, that is, the greedy pattern that is often said.
Copy codeThe Code is as follows:
'123'. match (/\ d {123456789}/g); // ["12345", "6789"]
Since there is a greedy pattern, there will certainly be a non-Greedy pattern, so that the regular expression matches as few as possible, that is to say, once a successful match does not continue to try, the approach is very simple. After the quantifiers are added? You can.
Copy codeThe Code is as follows:
'123'. match (/\ d {123456789 }? /G); // ["123", "456", "789"]
Group
Sometimes we want to match multiple characters when using quantifiers, instead of matching one character as in the above example. For example, we want to match a Byron string that appears 20 times, if we write Byron {20}, We will match Byro + n for 20 times. How can we take Byron as a whole? You can use () to achieve this goal, which is called grouping.
(Byron) {20}
What if I want to match Byron or Casper for 20 times? Can use characters | to achieve or
(Byron | Casper) {20}
We can see that there is a stuff in #1 in the figure. What is that? The Regular Expression of the Group puts the matching items in the group. By default, the matching items are distributed by number, and the captured group content is obtained by different numbers, this is useful in some functions that require specific operations on the matching items.
(Byron). (OK)
If grouping is nested, the number of the group outside is first
(^ | %>) [^ \ T] *)
Sometimes we don't want to capture some groups, but we just need to add? : No, it does not mean that the group content is not a regular expression, but it does not add a number to the group.
(? : Byron). (OK)
In fact, the group name can also be used in C # and other languages, but JavaScript does not support
Forward expression
Let's look at an example of good (? = Byron)
Copy codeThe Code is as follows:
(/Good (? = Byron)/cmd.exe c ('goodbyron123'); // ['good'] (/good (? = Byron)/cmd.exe c ('goodcasper123'); // null (/bad (? = Byron)/cmd.exe c ('goodcasper123'); // null
The above example shows that exp1 (? = Exp2) the expression will match the exp1 expression, but it will only match when the content following it is exp2, that is, two conditions, exp1 (?! Exp2) is similar
Good (?! Byron)
Copy codeThe Code is as follows:
(/Good (?! Byron)/cmd.exe c ('goodbyron123'); // null (/good (?! Byron)/cmd.exe c ('goodcasper123 '); // ['good'] (/bad (?! Byron)/cmd.exe c ('goodcasper123 '); // null
The above section describes the basic syntax (recommended) related to JavaScript Regular Expressions. I hope it will help you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!