Objective
Regular expressions are a very important and commonly used feature of JavaScript and are used very frequently in large frameworks such as jquery, recently taking time to learn about relevant knowledge, and to share it with friends who need it.
Mind Mapping
How REGEXP (regular expressions) are created
You can create a regexp in two ways, as follows:
Create regular Expressions by/..../(Note:/....../have no single or double quotes on either side)
To create a regular expression by regexp the constructor method
For better descriptive patterns, regular expressions provide 3 identifiers, respectively: g/i/m
G: Global match: Match in whole string, not after first match
I: Ignore case matching
M: For each row in a multiline string, apply special characters (^ and $) at the beginning and end of the line.
Look at the following code to deepen understanding:
var regx = new RegExp (' Are ', ' G ');
var regx1 =/are/g; Common ways to create
RegExp the primary properties of the instance
According to the RegExp constructor, we can probably also guess the main properties of RegExp, about the instance properties, and understand. However, it is important to note that these instance properties cannot be retrieved through for.
The following code can be used to deepen understanding:
var regx1 =/are/g; Common ways to create
Console.log ("Source:" +regx.source + "global:" +regx.global+ "IgnoreCase:" +regx.ignorecase + "multiline : "+regx.multiline);
Source:are global:true ignorecase:false multiline:false for
(var p in regx) {//does not enter the For loop
if ( Regx.hasownproperty (P)) {
console.log (regx[p]);
}
The main method of RegExp instance-test
This method is very simple, with only one parameter, which is often used to verify that the input parameter matches the regular expression pattern, or False if the match returns true. The following code can be used to deepen understanding:
var regx1 =/are/g;
var res = Regx.test (' You are a good boy! ');
Console.log (RES); True
var res1 = regx.test (' I am a good boy! ');
Console.log (RES1); False
The main method of RegExp instance-exec
The method is a very common method and needs to be understood well. It receives only one parameter, the string to match, and the return value is an array arr, which stores the information about the first match, including:
Input: String to match, enter value of Exec method
Index: The position of the match in the string
ARR[0]: pattern-matching string
Arr[1]...arr[n]: Nth Capturing group string
When using this method, note that if global flag G is not specified in the regular expression, each execution always returns the first match, and if the global flag G is set, each call to Exec will continue to find the new match in the string
The following code can be used to deepen understanding:
var regx =/fn: (\w+) \s+ln: (\w+) \s/g;
var s = "Your fn:xiaoxin Ln:tang right?";
var result = Regx.exec (s);
Console.log (Result.input); Your fn:xiaoxin Ln:tang right?
Console.log (Result.index); 5
Console.log (result[0]);//fn:xiaoxin Ln:tang
Console.log (result[1))//xiaoxin
Console.log (Result [2]); Tang
Console.log (result[3])//undefined print undefined because there are only 2 capturing groups
RegExp Constructor Properties
With regard to function properties, you can refer to the static properties of classes in other programming languages such as Java, which are shared by all regexp instances, which means that all regexp can access and modify these properties, and when an instance executes the test or Exec method, The values of these properties are also changed
Regarding these attributes, we can follow our own understanding of the memory:
Input: The string that requires pattern matching, the parameter of the test or Exec method. Parameter alias: $-
Lastmatch: Last match. Parameter alias:$&
Leftcontext: The string to the left of the match. Parameter alias: $ '
Rightcontext: The string to the right of the match. Parameter alias: $ '
1,2,$3: Captures the string corresponding to the group.
Of course, these values can be computed by the RegExp instance executing the exec return result, so why do you want to set these properties in the constructor regexp? *
The following code can be used to deepen understanding:
var regx =/fn: (\w+) \s+ln: (\w+) \s/g;
var s = "Your fn:xiaoxin Ln:tang right?";
var result = Regx.exec (s);
Console.log (Regexp.input); Your fn:xiaoxin Ln:tang right?
Console.log (Regexp.lastmatch); Fn:xiaoxin Ln:tang
Console.log (regexp.leftcontext);//your console.log
(regexp.rightcontext);//right?
Console.log (regexp.$1); Xiaoxin
Console.log (regexp.$2);//tang
REGEXP-Meta character
Similar to regular expressions in other languages, JS regular expressions also have a number of metacharacters, these characters have special purpose and meaning, so in the course of use, these characters need to escape, by adding ' \ ' before these characters to escape processing. JS Regular expression is the meta characters are:
( [ { \ ^ $ | ) ? * + . ] }
REGEXP-greedy match and lazy match
Greedy matching is when a regular expression is matched, the default is to make the matching length larger. In the JS regular expression, the lazy qualifier is '? ', and adding '? ' to the pattern requires lazy matching. Refer to the following code specifically to understand:
var s = ' I am a good boy,you are also a good boy! ';
var regx =/good.*boy/g; Greedy matching
console.log (regx.exec (s) [0]);//good boy,you are also a good boy
var regx1 =/good.*?boy/g;//lazy Match
con Sole.log (Regx1.exec (s) [0]); Good boy
The above is a small compilation for everyone to share the JavaScript regular expression and string regexp and string (i), the next article to share JavaScript regular expressions and strings RegExp and string (ii) I hope you like it.