REGEXP Essay JavaScript RegExp object _ Regular expression

Source: Internet
Author: User
Tags instance method

Overview

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

For an introduction to regular expressions, read the regular expression chapters in the JavaScript guide.

Grammar

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

Parameters

Pattern
The text of the regular expression
Flags
If specified, flags can have any combination of the following values:

G
Global match
I
Ignore case
M
Multiple lines; Let the start and end characters (^ and $) work in multiline mode (for example, ^ and $ can match the start and end of each line in a string (rows are split by \ n or \ r), not just the beginning and end of the entire input string.
U
Unicode. Treats the pattern as a sequence of Unicode code points (points).
Y
Viscosity In the target string, only the display position specified from the Lastindex property of the regular expression begins to match (and does not attempt to match from any subsequent index).
Describe
There are two ways to create a regular object: literal and constructor. To represent a string, the literal form does not use quotation marks, but the arguments passed to the constructor use quotation marks. 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 a compiled (compilation) state of the regular expression, using the literal amount when the regular expression is persisted as a constant. For example, when you construct a regular expression using literal literals in a loop, the regular expression is not recompiled in each iteration (recompiled).
The constructor for the regular expression object, such as the new RegExp (' Ab+c '), provides regular expression Run-time compilation (Runtime Compilation). If you know that the regular expression pattern will change, or that you do not know the pattern in advance, but get it from another source, such as user input, these situations can use the constructor.
Starting with ECMAScript 6, when the first argument is a regular expression and the second flag parameter is present, the new RegExp (/ab+c/, ' I ') no longer throws the exception of TypeError ("when constructed from other regular expressions that does not support flags"), instead, These parameters will be used to create a new regular expression.

When you use a constructor to create a regular object, you need a regular character escape rule (preceded by a backslash \). For example, the following are equivalent:

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

Defines a regular expression in literal quantities
var expression = /pattern/ flags;
The pattern section can be any simple or complex regular expression
Flage the behavior of regular Expressions 1.G: Global mode, does not stop 2.i after the first match is found: case-insensitive mode 3.M: Multiline mode
Cases:

var pattern1 =/at/g; Matches all at
var pattern2 =/[bc]at/i;//matches the first "bat" or "cat", case-insensitive
var pattern3 =/.at/gi//global match with. At "End" three characters. Do not distinguish between lowercase

All meta characters used in the pattern must be escaped. The metacharacters in the regular expression include: ([{\^$|? *+.}])
Cases:

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

Using the RegExp constructor, accept 2 parameters, Parameter 1: string pattern to match, parameter 2: Optional flag behavior
Cases:

var pattern5 = new RegExp ("[Bc]at", "I");

Note: Because the schema parameters of the RegExp constructor are strings, there are some cases where strings are to be double escaped. All meta characters must be escaped twice

Cases:
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: using literal and instantiated to create regular expressions is not the same as literal volume always shares the same regexp instance (ECMASCRIPT3). Each new RegExp instance created using the constructor is a new instance.

RegExp Instance Properties

Console.log (Pattern5.global); False whether the G-sign
Console.log (pattern5.ignorecase) is set, and whether the//true is set I flag
console.log (pattern5.multiline); False whether the M flag
Console.log (Pattern5.lastindex) is set, and//0 begins searching for the starting position of the next match
Console.log (pattern5.source);//[bc]at String representation of regular expressions

Inheritance Properties

Console.log (Pattern5.tostring ()); /[bc]at/i the literal representation of a regular expression
Console.log (pattern5.tolocalestring ());///[bc]at/i The literal representation of a regular expression represents
console.log ( Pattern5.valueof ()); /[bc]at/i the literal representation of a regular expression

RegExp instance method
Method one: Exec (), accepts a parameter, applies both the pattern string. Returns an array that contains the first occurrence information, returns NULL if not, and the returned array instance contains two property index (the position of the match in the character) and input (the string that applies the regular).

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 (), takes a parameter, applies both the pattern string. Returns true if the pattern matches the parameter, or false

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 ("Match succeeded");
} else {
Console.log ("Match failed");
}

constructor properties (not supported by some browsers)
Long property name Short attribute name Description
Input $_ the last string to match
Lastmatch $& last match
Lastparen $+ most recent capture group
Text before lastmatch in the leftcontext $ ' input string
Multiline $* Boolean, is multiline mode
Text after lastmatch in the rightcontext $ ' input string
$1~$9 is used to store the first few capturing groups, respectively

Limitations in ECMAScript
1. \a and \z anchors that match the start and end of a string
2. Find Backward
3. Parallel sets and intersection classes
4. Atomic Group
5.Unicode support (except single character)
6. Named Capture Group
7.S and x Match mode
8. Conditional matching
9. Regular expression annotations

Just found a way to match multiple rows 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 not be used, many times it is more convenient to use DW.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.