Dom Notes (10): JavaScript Regular expressions

Source: Internet
Author: User
Tags alphabetic character first string

First, REGEXP

ECMAScript supports regular expressions through the RegExp type class and is similar to Perl in syntax:

var exp =/pattern/flags;

The PATTERNB section is any simple or complex regular expression; flags is one or more flags for each regular expression.

Pattern matching for regular expressions supports three flags:

G: Global mode, where the pattern is applied to the entire string, rather than stopping immediately when the first occurrence is found

I: Case-insensitive mode

M: Multiline mode, that is, the end of a line of text is also going to continue tea find out if there are items in the next row that match the pattern.

1. Create Regular expressions

JavaScript creates regular representations in two ways: literal creation and RegExp object creation.

literal creation var pattern1 =/[bc]at/i;var pattern2 =/\[bc\]at/ig;//regexp object created var pattern3 = new RegExp ("[Bc]at", "I");  With pattern1 equivalent var pattern4 = new RegExp ("\\[bc\\]at", "IG"); Equivalent to PATTERN2

There are two ways to differentiate between the two: the conversion of special characters and the creation of instances.

The schema parameter of the RegExp constructor is a string, so you need to double-escape the special characters, with the following differences:

/\w\\hello/

Literal mode An equivalent string
/\[bc\]at/ "\\[bc\\]at"
/\d.\d{1,2}/ "\\d.\\d{1,2}"
/\w\\hello/ "\\w\\\\hello"

In ECMAScript 3, the literal form shares a regexp instance, and each instance of the constructor is different

var re = null, I;for (i=0;i<3;i++) {re =/cat/g; Re.test ("Catastrophe");}     for (i=0;i<3;i++) {re = new RegExp ("Cat", "G"); Re.test ("Catastrophe");}

In a low-version browser, such as IE6, only one pop-up is true for the first loop, and the rest is false. The second loop pops up true.

In ECMAScript 5, it is stated that the use of regular expression literals is the same as using the RegExp constructor, with each invocation creating a new instance . So two loops in the modern browser all pop up true.

2. RegExp Instance Properties

global Boolean value, whether set G flag
ignorecase Boolean value, whether the I flag is set
multiline Boolean value, whether the M flag is set
lastindex integer, the character position at which the next match begins
source

3. Methods

There are two common methods:exec () and test (), all of which accept a string parameter .

If there is a match, exec () returns an array with two additional attributes: Index and input. Index indicates the position of the match in the string, and input represents the source string, which is the parameter of the exec () . In the array, the first item is a string that matches the entire pattern, and the other items match the capturing group (only one if no capturing group is included). No match exists, and exec () returns NULL.

Test () indicates whether a match exists in the string, there is a return of true, and there is no return false.

var text = "Mom and dad and bady"; var pattern =/mom (and dad (and Bady)?)?    /gi;var matches = pattern.exec (text); alert (matches.length);     3alert (Matches.index);     0alert (Matches.input);        Mom and Dad and Badyalert (Matches[0]);        Mom and Dad and Badyalert (Matches[1]);        And dad and Badyalert (matches[2]); and Bady

For exec (), if the global flag G is not set, multiple calls to Exec () on the same string will always return information for the first occurrence, and if global flag G is set, each call to EXEC () continues to find the string from the last matching location.

Ii. type of string

String types are object wrapper types for strings, like number, Boolean, etc. (Dom Note (ix): reference type, basic wrapper type, and monomer built-in object), or you can use new to create a string. Pattern matching is useful in string processing, and the string type defines multiple methods associated with it.

Match (Pattern): pattern is a literal regular expression or RegExp object, essentially the same as the Exec () method.

var text = "Cat,bat,sat,fat"; var pattern =/.at/;var matches = Text.match (pattern);//var matches = pattern.exec (text); Aler T (matches.index); alert (matches[0]); alert (Pattern.lastindex);

Search (Pattern): The parameter is the same as match (), finds from the beginning, returns the index of the first occurrence, and returns 1 if there is no match.

var text = "Cat,bat,sat,fat"; var pos = Text.search (/at/); alert (POS); 1

Replace (oldstring,newstring): Replaces oldstring with newstring. The first string can be a pattern object, and the second string can be used in conjunction with a capturing group, or a function.

var text = "Cat,bat,sat,fat"; var pattern =/(. at)/g;var re = text.replace (pattern, "word"); alert (re); Word (cat), Word (BAT), Word (Sat), Word (FAT)

If no capturing group is in the pattern, use an empty string instead.

If the second argument is a function, the function receives three arguments: The pattern match, the position of the pattern match in the string, and the original string.

Function htmlescape (text) {    return text.replace (/[<> "&]/g,function ( Match,pos,text)         {             switch (Match)              {                case   "<":                     return  "&lt;";                 case  " > ":                     return  "&gt;";                 case  " & ":                     return  "&amp;";                 case  "\" " :                     return  "&quot;";             }         });} Returns: &lt;p class=&quot;greeting&quot;&gt;helloworld&lt;/p&gt;alert (HtmlEscape (" <p class=\ "greeting\" >helloWorld</p> "));

Jo Zheng When multiple capturing groups are defined in an expression, the one that is passed to the function is the match, the first capturing group, the second capturing group .... The last two parameters are unchanged.

Split (String[,limit]): Separates the string and returns an array. The string can be a normal string, or it can be a pattern-matching object. An optional limit indicates the size of the returned array

var colortext = "Red,blue,yellow,black";   Alert (Colortext.split (","));  [Red,blue,yellow,black]alert (Colortext.split (",", 2));  [Red,blue]alert (Colortext.split (/\w/)); [Red,blue,yellow,black]

Rules of Regular expressions

Character

Description

\

Marks the next character as a special character, or a literal character, or a backward reference, or an octal escape. For example, "n" matches the character "n". "\ n" matches a line break. The sequence "\ \" matches "\" and "\ (" Matches "(".

^

Matches the starting position of the input string. If the multiline property of the RegExp object is set, ^ also matches the position after "\ n" or "\ r".

$

Matches the end position of the input string. If the multiline property of the RegExp object is set, $ also matches the position before "\ n" or "\ r".

*

Matches the preceding subexpression 0 or more times. For example, zo* can match "z" and "Zoo". * Equivalent to {0,}.

+

Matches the preceding subexpression one or more times. For example, "zo+" can Match "Zo" and "Zoo", but not "Z". + equivalent to {1,}.

?

Matches the preceding subexpression 0 or one time. For example, "Do (es)?" You can match "do" in "do" or "does".?

N

N is a non-negative integer. Matches the determined n times. For example, "o{2}" cannot match "O" in "Bob", but can match two o in "food".

{N,}

N is a non-negative integer. Match at least n times. For example, "o{2,}" cannot match "O" in "Bob", but can match all o in "Foooood". "O{1,}" is equivalent to "o+". "O{0,}" is equivalent to "o*".

{N,m}

Both M and n are non-negative integers, where n<=m. Matches at least n times and matches up to M times. For example, "o{1,3}" will match the first three o in "Fooooood". "o{0,1}" is equivalent to "O?". Note that there can be no spaces between a comma and two numbers.

?

When the character immediately follows any other restriction (*,+,?,{n},{n,},{n,m}), the matching pattern is non-greedy. The non-greedy pattern matches the searched string as little as possible, while the default greedy pattern matches as many of the searched strings as possible. For example, for the string "Oooo", "o+?" A single "O" will be matched, and "o+" will match all "O".

.

Matches any single character except "\ n". To match any character that includes "\ n", use a pattern like "[. \ n]".

(pattern)

Match pattern and get this match. The obtained matches can be obtained from the resulting matches collection, the Submatches collection is used in VBScript, and the $0...$9 property is used in JScript. To match the parentheses character, use "\ (" or "\").

(?:p Attern)

Matches pattern but does not get a matching result, which means that this is a non-fetch match and is not stored for later use. This is used in the or character "(|)" It is useful to combine the various parts of a pattern. For example, "Industr (?: y|ies)" is a more abbreviated expression than "industry|industries".

(? =pattern)

Forward-checking matches the lookup string at the beginning of any string that matches the pattern. This is a non-fetch match, which means that the match does not need to be acquired for later use. For example, "Windows (? =95|98| nt|2000) "Can match" windows "in" Windows2000 ", but does not match" windows "in" Windows3.1 ". Pre-checking does not consume characters, that is, after a match occurs, the next matching search starts immediately after the last match, rather than starting with the character that contains the pre-check.

(?! Pattern

A negative pre-check matches the lookup string at the beginning of any string that does not match the pattern. This is a non-fetch match, which means that the match does not need to be acquired for later use. For example, "Windows (?! 95|98| nt|2000) "Can match" windows "in" Windows3.1 ", but does not match" windows "in" Windows2000 ". Pre-check does not consume characters, that is, after a match occurs, the next matching search starts immediately after the last match, rather than starting with the character that contains the pre-check

X|y

Match x or Y. For example, "Z|food" can match "Z" or "food". "(z|f) Ood" matches "Zood" or "food".

[XYZ]

The character set is combined. Matches any one of the characters contained. For example, "[ABC]" can Match "a" in "plain".

[^XYZ]

Negative character set. Matches any character that is not contained. For example, "[^ABC]" can match "P" in "plain".

[A-z]

The character range. Matches any character within the specified range. For example, "[A-z]" can match any lowercase alphabetic character in the range "a" to "Z".

[^a-z]

A negative character range. Matches any character that is not in the specified range. For example, "[^a-z]" can match any character that is not in the range "a" to "Z".

\b

Matches a word boundary, which is the position between a word and a space. For example, "er\b" can Match "er" in "never", but cannot match "er" in "verb".

\b

Matches a non-word boundary. "er\b" can Match "er" in "verb", but cannot match "er" in "Never".

\d

Matches a numeric character. equivalent to [0-9].

\d

Matches a non-numeric character. equivalent to [^0-9].

\s

Matches any whitespace character, including spaces, tabs, page breaks, and so on. equivalent to [\f\n\r\t\v].

\s

Matches any non-whitespace character. equivalent to [^\f\n\r\t\v].

\w

Matches any word character that includes an underscore. Equivalent to "[a-za-z0-9_]"

\w

Matches any non-word character. Equivalent to "[^a-za-z0-9_]".

\f, \ n, \ r, \ t, \v

Matches a page break, line break, carriage return, Horizontal tab, Vertical tab, respectively, equivalent to \x0c and \CL, \x0a and \CJ, \x0d and \cm, \x09 and \ci, \x0b, and \ck

\cx

Matches the control character indicated by X. For example, \cm matches a control-m or carriage return. The value of x must be one of a-Z or a-Z. Otherwise, c is considered to be a literal "C" character.

\xn

Match N, where n is the hexadecimal escape value. The hexadecimal escape value must be two digits long for a determination. For example, "\x41" matches "A". "\x041" is equivalent to "\x04&1". ASCII encoding can be used in regular expressions.

\num

Matches num, where num is a positive integer. A reference to the obtained match. For example, "(.) \1 "matches two consecutive identical characters.

\ n

Identifies an octal escape value or a backward reference. n is a backward reference if \ n is preceded by at least one of the sub-expressions obtained. Otherwise, if n is the octal number (0-7), N is an octal escape value.

\nm

Identifies an octal escape value or a backward reference. If at least NM has obtained a subexpression before \nm, then NM is a backward reference. If there are at least N fetches before \nm, then n is a backward reference followed by the literal m. If none of the preceding conditions are met, if both N and M are octal digits (0-7), then \nm will match the octal escape value nm.

\nml

If n is an octal number (0-3) and both M and L are octal digits (0-7), the octal escape value NML is matched.

\un

Match N, where N is a Unicode character represented by four hexadecimal digits. For example, \u00a9 matches the copyright symbol (?).

Iv. Common Regular Expressions : common Regular Expression induction

Original starting: http://www.ido321.com/1355.html


Dom Notes (10): JavaScript Regular expressions

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.