A global matching pattern for JS/g expressions--detailed

Source: Internet
Author: User
Tags lowercase
  code is as follows copy code

var str = " 123#abc ";
var re =/abc/ig;
Console.log (Re.test (str)),//output ture
Console.log (re.test (str)),//Output False
Console.log (str); Output ture
Console.log (re.test (str));//Output false

If you use the "G" identifier when you create a regular expression object, or if you set its Global property value to Ture, the newly created regular expression object will use the pattern to match the string that you want to match globally. In global match mode, you can perform multiple matches on the string that you specify to find. Each match uses the value of the Lastindex property of the current regular object as the starting position to start the lookup in the target string. The initial value of the Lastindex property is 0, and after the matching item is found, the value of lastindex is reset to the position index of the next character in the string that matches the content, which identifies where to start the next time the match is performed. If no matching item is found, the value of lastindex is set to 0. When no global match flag for a regular object is set, the value of the Lastindex property is always 0, and each execution match finds only the first matching item in the string. The following code can be used to view the values that match the corresponding lastindex property in the execution.

The code is as follows Copy Code
var str = "123#ABC";
var re =/abc/ig;
Console.log (Re.test (str)); Output ture
Console.log (Re.lastindex); Output 7
Console.log (Re.test (str)); Output false
Console.log (Re.lastindex); Output 0
Console.log (Re.test (str)); Output ture
Console.log (Re.lastindex); Output 7
Console.log (Re.test (str)); Output false
Console.log (Re.lastindex); Output 0

Modifiers

modifier description
i performs a match that is not case sensitive.
G performs a global match (finds all matches rather than stops after the first match is found).
m performs multiple-line matching.

Brackets

Square brackets are used to find characters in a range:

An expression Description
[ABC] Find any character between square brackets.
[^ABC] Finds any characters that are not between brackets.
[0-9] Look for any numbers from 0 to 9.
[A-z] Finds any character that writes from A to lowercase z from small.
[A-z] Finds any characters from uppercase A to uppercase Z.
[A-z] Finds any characters from uppercase A to lowercase z.
[ADGK] Finds any character within a given set.
[^ADGK] Finds any character outside the given collection.
(Red|blue|green) Finds any of the specified options.

Metacharacters

Metacharacters (metacharacter) is a character that has a special meaning:

Meta character Description
. Finds a single character, except for line breaks and line terminator.
W Find word characters.
W Find non word characters.
D Find numbers.
D Looks up a non-numeric character.
S Find white space characters.
S Find Non-white-space characters.
B Finds a match at the beginning or end of a word.
B Finds a match that is not at the beginning or end of a word.
Find NUL characters.
N Find line breaks.
F Find page breaks.
R Find a return character.
T Find tabs.
V Finds vertical tabs.
Xxx Find the characters specified in octal xxx.
xDD Finds characters that are specified in the hexadecimal number DD.
Uxxxx Finds Unicode characters as specified in hexadecimal number xxxx.

quantifier

The The
quantifier description
n+ matches any string that contains at least one n. The
N. matches any string containing 0 or more n.
n? matches any string that contains 0 or one n. The
n{x} matches a string that contains a sequence of X N. The
n{x,y} matches a string containing a sequence of X or Y N. The
n{x,} matches a string containing at least X-n sequences. The
n$ matches any string ending with N. The
^n matches any string that starts with N. The
? =n matches any string immediately followed by the specified string n.
?! n matches any string that is not followed by the specified string n.

RegExp Object Properties

Ff:firefox, ie:internet Explorer

The
Properties Description FF IE
global REGEXP object has flag G. 1 4
ignoreCase RegExp object has flag I. 1 4
lastindex An integer that marks the next occurrence of the character position. 1 4
Multiline RegExp object has flag m. The source text of the 1 4
source Regular expression. 1 4

RegExp Object method

Ff:firefox, ie:internet Explorer

The
Method Description FF IE
compile compiles a regular expression. 1 4
exec Retrieves the value specified in the string. Returns the found value and determines its location. 1 4
Test Retrieves the value specified in the string. Returns TRUE or FALSE. 1 4

Methods that support the String object of a regular expression

Ff:firefox, ie:internet Explorer

The
Method Description FF IE
Search retrieves the values that match the regular expression. 1 4
match finds a match for one or more regular expressions. 1 4
replace replaces substrings that match regular expressions. 1 4
split splits the string into an array of strings.

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.