English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Sketch of RegExp - JavaScript RegExp Object

Overview

The constructor of RegExp creates a regular expression object that matches text with a pattern.

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

Syntax

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

Parameters

pattern
Text of the regular expression
flags
If specified, the flag can be any combination of the following values:

g
Global match
i
Case-insensitive
m
Multiline; Allow the start and end characters (^ and $) to work in multiline mode (for example, ^ and $ can match the start and end of each line in the string, separated by \n or \r, not just the very beginning and end of the entire input string).
u
Unicode. Treat the pattern as a sequence of Unicode code points (code points).
y
粘度; In the target string, matching starts only from the display position specified by the lastIndex property of the regular expression (and does not attempt to match from any subsequent index).
Description
There are two ways to create a regular expression object: literals and constructors. To represent a string, the literal form does not use quotes, while the parameters passed to the constructor 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 literals are used when the regular expression is kept as a constant. For example, when you use literals to construct a regular expression within a loop, the regular expression is not recompiled (recompiled) with each iteration.
The constructor of the regular expression object, such as new RegExp('ab}}+c') provides runtime compilation of regular expressions (runtime compilation). If you know that the regular expression pattern will change, or if you do not know what pattern will be used in advance but will be obtained from another source, such as user input, these situations can use the constructor.
from ECMAScript 6Starting, when the first parameter is a regular expression and the second flag parameter exists, new RegExp("/ab+c/, 'i') no longer throws a TypeError ("Not supported flag when constructing from other regular expressions") exception, instead, a new regular expression will be created using these parameters.

When creating a regular expression object with the constructor, the conventional character escaping rules (prefix with a backslash \) must be used. For example, the following are equivalent:

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

defines a regular expression by literal
var expression = /pattern/ flags;
pattern part can be any simple or complex regular expression
flag indicates the behavior of the regular expression 1.g:Global mode, will not stop after finding the first match 2.i:Case insensitive mode 3.m:Multiline mode
Example:

var pattern1 = /at/g; //matches all "at" in the string
var pattern2 = /[bc]at/i; //matches the first "bat" or "cat", case insensitive
var pattern3 = /.at/gi; //Global match for three characters ending with .at. Case insensitive

All meta characters used in the pattern must be escaped. Meta characters in regular expressions include:([{\^$|?*+.}])
Example:

var pattern4 = /\[bc\]at/i; //matches the first "[bc]at", case insensitive

uses the RegExp constructor, accepts2个参数,参数1:The string pattern to be matched, parameter2:Optional flag behavior
Example:

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

Note: Since the pattern parameter of the RegExp constructor is always a string, in some cases, the string needs to be doubly escaped. All meta characters must be doubly escaped

Example:
Literal        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 instantiation is different, literals will always share the same RegExp instance (ECMAScript3). Each new RegExp instance created by the constructor is a new instance.

RegExp instance properties

console.log(pattern5.global); //false Whether the g flag is set
console.log(pattern5.ignoreCase); //true Whether the i flag is set
console.log(pattern5.multiline); //false Whether the m flag is set
console.log(pattern5.lastIndex); //0 The starting position to search for the next match
console.log(pattern5.source); //[bc]at The string representation of the regular expression

Inherited properties

console.log(pattern5.toString()); // /[bc]at/i The literal representation of the regular expression
console.log(pattern5.toLocaleString()); // /[bc]at/i The literal representation of the regular expression
console.log(pattern5.valueOf()); // /[bc]at/i The literal representation of the regular expression

RegExp instance methods
Method one: exec(), accepts a parameter, which is the pattern string. It returns an array containing the information of the first match, or null if there is no match. The returned array instance contains two properties: index (the position of the match in the string) and input (the string applied to the regular expression).

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 two: test(), accepts a parameter, which is the pattern string. It returns true if the pattern matches the parameter, otherwise false

var text2 = "000-00-0000";
var pattern9 = new RegExp("\\d{3}-2}-4}
console.log(pattern9.test(text2))
console.log(text2);
if (pattern9.test(text2)) {
console.log("Match succeeded");
} else {
console.log("Match failed");
}

Constructor properties (not supported by some browsers)
Long attribute name    Short attribute name    Description
input      $_    The last string to be matched
lastMatch    $&    The most recent match item
lastParen    $+    The most recent capture group
leftContext    $`    Text before lastMatch in input string
multiline    $*    Boolean, whether it is a multiline mode
rightContext $'    Text after lastMatch in input string
        $1~$9 Used to store the nth capture group

Within the Limitations of ECMAScript
1. Match String Start and End Anchors \A and \Z
2. Lookahead
3. Union and Intersection Classes
4. Atomic Groups
5. Unicode Support (Single Characters Excluded)
6. Named Capture Groups
7. s and x Matching Pattern
8. Conditional Matching
9. Regular Expression Comment

Just found a method to match multiline 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 cannot be used, and it is more convenient to use dw most of the time.

You May Also Like