RegExp Essay JavaScript RegExp Object

Time:2019-10-7

Summary

RegExp‘s constructor creates a regular expression object that matches text with patterns.

For an introduction to regular expressions, read the regular expressions chapter in the JavaScript Guide.

grammar

Text and construction symbols are possible:
/pattern/flags new RegExp(pattern [, flags])

parameter

pattern
Text of regular expressions
flags
If specified, the flag may have any combination of the following values:

g
Global matching
i
ignore case
m
Multi-line; let start and end characters (^ and $) work in multi-line mode (for example, ^ and $match the beginning and end of each line in the string (lines are split by \ n or r), not just the beginning and end of the entire input string.
u
Unicode. Treat a pattern as a sequence of Unicode code points.
y
Viscosity; in the target string, only matches start at the display position specified by the lastIndex attribute of the regular expression (and no subsequent index matches are attempted).
describe
There are two ways to create a regular object: literal quantities and constructors. To represent a string, literal quantities do not use quotes, but parameters passed to constructors use quotes. The following expression creates the same regular expression:


/ab+c/i;
new RegExp('ab+c', 'i');
new RegExp(/ab+c/, 'i');

When an expression is assigned, the literal form provides the compilation state of the regular expression, and the literal form is used when the regular expression remains constant. For example, when you use literals to construct a regular expression in a loop, the regular expression will not be recompiled in each iteration.
Constructors of regular expression objects, such as new RegExp (‘ab + c’), provide runtime compilation of regular expressions. If you know that the regular expression pattern will change, or you do not know any pattern in advance, instead, you can get it from another source, such as user input, which can use constructors.
Beginning with ECMAScript 6, when the first parameter is a regular expression and the second flag parameter exists, new RegExp (/ab+c/,’i’) no longer throws an exception to TypeError (“Flags are not supported when constructed from other regular expressions”), instead, it will use these parameters to create a new regular expression.

When using constructors to create regular objects, regular character escape rules (with backslashes in front) are required. For example, the following are equivalent:


var re = new RegExp("\w+");
var re = /\w+/;

Define regular expressions literally
var expression = /pattern/ flags;
The pattern part can be any simple or complex regular representation.
Flage indicates the behavior of regular expressions 1.g: global mode, and does not stop 2.i: case-insensitive mode 3.m: multi-line mode after discovering the first match
Example:

Var pattern 1 = at / g; // matches all at in the string
Var pattern 2 =/[bc]at/i; //matches the first bat or cat, case-insensitive
Var pattern 3 = /.at/gi; //global matching with three characters ending with.at. Indiscriminate lowercase

All metacharacters used in the schema must be escaped. Metacharacters in regular expressions include: ([{^$|?*+.})
Example:

Var pattern 4 = /[bc] at/i; // matches the first "[bc] at", case-insensitive

Using RegExp constructor, two parameters are accepted, parameter 1: string pattern to match, parameter 2: optional flag behavior
Example:


var pattern5 = new RegExp("[bc]at", "i");

Note: Because the pattern parameters of the RegExp constructor are strings, in some cases the strings need to be double escaped. All metacharacters must be double escaped

Example:
Literal Quantity: Equivalent String
/\[bc\]at/      “\\[bc\\]at”
/\.at/        “\\.at”
/name/\age/    “name\\/age”
/\d.\d{1,2}/    “\\d.\\d{1,2}”
/\w\\hello\\123/ “\\w\\\\hello\\\\123”

Note: Creating regular expressions with literals and instantiations is different, and literals always share the same RegExp instance (ECMAScript3). Each new RegExp instance created with the constructor is a new instance.

RegExp instance attribute

Console. log (pattern 5. global); //false set G flag
Console. log (pattern5. ignoreCase); //true set I flag
Console. log (pattern 5. multiline); //false set the m flag
Console. log (pattern 5. lastIndex); //0 starts searching for the starting position of the next match
Console. log (pattern 5. source); //[bc] at regular expression string representation

Inheritance attribute

Console. log (pattern5. toString (); /// [bc] at / I regular expression literal representation
Console. log (pattern5. toLocaleString ()); /// [bc] at/i regular expression literal representation
Console. log (pattern 5. valueOf ()); /// [bc] at / I regular expression literal representation

RegExp Example Method
Method 1: exec (), accepts a parameter, which applies the pattern string. Returns an array containing the first matching information, and returns null without it. The returned array instance contains two attribute indexes (the position of the matching item in the character) and input (the string with regular application).


var text = "huang jin liang shi ge hao ren";
var pattern6 = new RegExp("huang( jin liAng( shi ge hao ren)?)?", "i");
var matches = pattern6.exec(text);
console.log(matches); 
//[ 'huang jin liang shi ge hao ren',
// ' jin liang shi ge hao ren',
// ' shi ge hao ren',
// index: 0,
// input: 'huang jin liang shi ge hao ren' ]

var text1 = "cat, bat, sat";
var pattern7 = new RegExp(".at")
var matches1 = pattern7.exec(text1);
console.log(matches1); //cat

var pattern8 = new RegExp(".at", "gm");
var matches2 = pattern8.exec(text1);
console.log(matches2); //cat
var matches3 = pattern8.exec(text1);
console.log(matches3); //bat
var matches4 = pattern8.exec(text1);
console.log(matches4); //sat
var matches5 = pattern8.exec(text1);
console.log(matches5); //null

Method 2: test (), which accepts a parameter, applies the pattern string. This pattern returns true if it matches this parameter, and vice versa

var text2 = "000-00-0000";
var pattern9 = new RegExp("\d{3}-\d{2}-\d{4}");
console.log(pattern9.test(text2))
console.log(text2);
if (pattern9.test(text2)) {
Console. log ("successful matching");
} else {
Console. log ("matching failure");
}

Constructor properties (not supported by some browsers)
Long Attribute Name Short Attribute Name Description
Input $The last string to match
LastMatch $& last match
LastParen $+) Last capture group
The text before last Match in the leftContext $`input string
Multline $* Boolean, is it multiline mode?
Text after lastMatch in the rightContext $”input string
The $1 to $9 is used to store the capture groups, respectively

Limitations in ECMAScript
1. AnchorA andZ matching the beginning and end of a string
2. Look back
3. Union and intersection classes
4. atomic group
5. Unicode support (except for single characters)
6. Named capture group
7.s and X matching patterns
8. Conditional Matching
9. Regular expression annotations

Just found a method of matching multiple lines in JS


<script>
var s = "Please yes\nmake my day!";
alert(s.match(/yes.*day/));
// Returns null
alert(s.match(/yes[^]*day/));
// Returns 'yes\nmake my day'
</script>

Unfortunately, EDITPLUS can’t be used. It’s more convenient to use DW in many cases.