Javascript Regular Expression & lt; 1 & gt;

Source: Internet
Author: User

Regular expression syntax
A regular expression is a text mode consisting of common characters (such as characters a to z) and special characters (such as metacharacters. This mode describes one or more strings to be matched when searching the text subject. A regular expression is used as a template to match a character pattern with the searched string.
The following table provides a complete reference list for metacharacters and their behavior in the context of a regular expression:

Character Description
\ Escape Character. We used this character before, that is, some characters have special meanings and escape it to make it a common character, it is used on common characters to indicate its special meaning.
^ Matches the start position of the input string. If multiple rows (m) are matched, the start of the row is also matched.
$ Matches the end position of the input string. If multiple rows (m) are matched, the end of the row is also matched.
* Matches the previous subexpression zero or multiple times. For example, zo * can match "z" and "zoo ". * Is equivalent to {0 ,}.
+ Match the previous subexpression once or multiple times. For example, 'Zo + 'can match "zo" and "zoo", but cannot match "z ". + Is equivalent to {1 ,}.
? Match the previous subexpression zero or once. For example, "do (es )? "Can match" do "in" do "or" does ".? It is equivalent to {0, 1 }.
{N} NIs a non-negative integer. MatchedNTimes. For example, 'O {2} 'cannot match 'O' in "Bob", but can match two o in "food.
{N,} NIs a non-negative integer. At least matchNTimes. 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} MAndNAll are non-negative integers, whereN<=M. Least matchNTimes and most matchingMTimes. Liu, "o {1, 3}" will match the first three o in "fooooood. 'O {0, 1} 'is equivalent to 'o? '. Note that there must be no space between a comma and two numbers.
? When this character is followed by any other delimiter (*, + ,?, {N},{N,},{N,M}) The matching mode is not greedy. The non-Greedy mode matches as few searched strings as possible, while the default greedy mode matches as many searched strings as possible. For example, for strings "oooo", 'O ++? 'Will match a single "o", and 'O +' will match all 'O '.
. Matches any single character except "\ n. To match any character including '\ n', use a pattern like' [. \ n.
(Pattern) MatchPatternAnd obtain the matching. The obtained match can be obtained from the generated Matches set.$0...$9Attribute. To match the parentheses, use '\ (' or '\)'.
(? :Pattern) MatchPatternBut does not get the matching result, that is, this is a non-get match and is not stored for future use. This is useful when you use the "or" character (|) to combine each part of a pattern. For example, 'industr (? : Y | ies) is a simpler expression than 'industry | industries.
(? =Pattern) Forward pre-query, in any matchPatternTo start from the string. This is a non-get match, that is, the match does not need to be obtained for future use. For example, 'windows (? = 95 | 98 | NT | 2000) 'can match "Windows" in "Windows 2000", but cannot match "Windows" in "Windows 3.1 ". Pre-query does not consume characters, that is, after a match occurs, the next matching search starts immediately after the last match, instead of starting after the pre-query characters.
(?!Pattern) Negative pre-query, in any MismatchPatternTo start from the string. This is a non-get match, that is, the match does not need to be obtained for future use. For example, 'windows (?! 95 | 98 | NT | 2000) 'can match "Windows" in "Windows 3.1", but cannot match "Windows" in "Windows 2000 ". Pre-query does not consume characters. That is to say, after a match occurs, the next matching search starts immediately after the last match, instead of starting after the pre-query characters.
X|Y MatchXOrY. For example, 'z | food' can match "z" or "food ". '(Z | f) ood' matches "zood" or "food ".
[Xyz] Character Set combination. Match any character in it. For example, '[abc]' can match 'A' in "plain '.
[^Xyz] Negative value character set combination. Match any character not included. For example, '[^ abc]' can match 'p' in "plain '.
[A-z] Character range. Matches any character in the specified range. For example, '[a-z]' can match any lowercase letter in the range of 'A' to 'Z.
[^A-z] Negative character range. Matches any character that is not within the specified range. For example, '[^ a-z]' can match any character that is not in the range of 'A' to 'Z.
\ B Match A Word boundary, that 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 Match non-word boundary. 'Er \ B 'can match 'er' in "verb", but cannot match 'er 'in "never '.
\ CX MatchingXThe specified control character. For example, \ cM matches a Control-M or carriage return character.XMust be a A-Z or one of a-z. Otherwise, c is treated as an original 'C' character.
\ D Match a numeric character. It is equivalent to [0-9].
\ D Match a non-numeric character. It is equivalent to [^ 0-9].
\ F Match a form feed. It is equivalent to \ x0c and \ cL.
\ N Match A linefeed. It is equivalent to \ x0a and \ cJ.
\ R Match a carriage return. It is equivalent to \ x0d and \ cM.
\ S Matches any blank characters, including spaces, tabs, and page breaks. It is equivalent [? \ F \ n \ r \ t \ v].
\ S Match any non-blank characters. It is equivalent to [^? \ F \ n \ r \ t \ v].
\ T Match a tab. It is equivalent to \ x09 and \ cI.
\ V Match a vertical tab. It is equivalent to \ x0b and \ cK.
\ W Match any word characters that contain underscores. It is equivalent to '[A-Za-z0-9 _]'.
\ W Match any non-word characters. It is equivalent to '[^ A-Za-z0-9 _]'.
\ XN MatchN, WhereNIt is a hexadecimal escape value. The hexadecimal escape value must be determined by the length of two numbers. For example, '\ x41' matches "". '\ X041' is equivalent to '\ x04' & "1 ". The regular expression can use ASCII encoding ..
\Num MatchNum, WhereNumIs a positive integer. References to the obtained matching. For example, '(.) \ 1' matches two consecutive identical characters.
\N Identifies an octal escape value or a backward reference. If \NAt leastNObtained subexpressionsNIs backward reference. Otherwise, ifNIs an octal digit (0-7 ),NIt is an octal escape value.
\Nm Identifies an octal escape value or a backward reference. If \NmAt least is preceded by at leastNmObtain the child expression, thenNmIs backward reference. If \NmAt leastNNIs followed by textM. If none of the preceding conditions are met, if?NAndMAll are Octal numbers (0-7), then \NmMatch the octal escape ValueNm.
\Nml IfNIt is an octal digit (0-3) andMAndLIf the values are Octal numbers (0-7), the octal escape value is matched.Nml.
\ UN MatchN, WhereNIt is a Unicode character represented by four hexadecimal numbers. For example, \ u00A9 matches the copyright symbol (?).
 


Create Regular Expression
Var re1 = new RegExp ("");
Var re2 =/\ d {2 }/;
Alert (re1.test ("a"); // true
Alert (re2.test ("wa12le"); // true
The first parameter of the RegExp constructor is the text content of the regular expression, and the first parameter is an optional flag. The flag can be used in combination.
G (full-text search)
I (Case Insensitive)
M (multi-row search)
Var re1 = new RegExp ("a", "I ");
Methods and attributes related to regular expressions
Regular Expression object Method
• Test: returns a Boolean value indicating whether the pattern exists in the searched string. If yes, true is returned. Otherwise, false is returned.
• Exec: run the search in the string in regular expression mode and return an array containing the search result.
• Compile, which compiles regular expressions into internal formats for faster execution. // This is rarely used at present, and Google browser support is problematic.
Test the usage of a regular expression:
The test method above only knows whether the string matches the pattern. What if we need to know which characters match the pattern?
The array 1st to nelement returned by exec contains any child matching in the match.
1 var version = "XunLei. Chinese 2012 ";
2 var re =/^ [a-z] + \. + [a-z] + \ s + \ d + $/I;
3 alert (re. test (version); // true matches successfully.
4 // The problem is how to know the version number, using exec
5 alert(re.exe c (version); // XunLei. China 2012 is returned directly because no value is captured at this time.
6 // in this way, we only need to add () to the version number to save the capture.
7 var re1 =/^ [a-z] + \. + [a-z] + \ s + (\ d +) $/I;
8 var verArr=re1.exe c (version );
9 alert (verArr [0]); // All Information
10 alert (verArr [1]); // version number
11 // a simple method is to redefine a regular expression to directly obtain the numerical part.
12 var re2 =/\ d + /;
13 alert(re2.exe c (version); // return 2012
Remove row number
In addition, when the string does not match the re, the exec method returns null.
Some methods related to the regular expression of the String object
Match.
Replace: replace the substring that matches the regular expression.
Search to retrieve the value that matches the regular expression.
Split: splits the string into a string array.
1 var str = "Hello Rohelm! ";
2 alert (str. replace ("Rohelm", "everyone"); // return Hello everyone.
3 // The method we usually use above. Of course we can think of the string operation method of regular classes in C #
4 // The first parameter of replace can be a regular expression.
5 var re =/\ s +/; // blank character www.2cto.com
6 // The regular expression is extremely convenient when you do not know how many blank characters are in the string
7 alert (str. replace (re, "^ _ × "));
8 var str = "2012-12-25 ";
9 var arr = str. split ("-"); // Returns ["2012", "12", "25"]
10 alert (arr [0] + "year" + arr [1] + "month" + arr [2] + "day ");
11 // we can obtain the time in this way, but the user may input any time, such as 2012 2 21 or 2012-2 21.
12 var re1 =/[^ 0-9] +/; // used to match unpredictable input whitelist
13 var str1 = "2012 + 2% 21 ";
14 var arr1 = str1.split (re1); // splits the string according to the rules specified by the Regular Expression
15 alert (arr1 [0] + "year" + arr1 [1] + "month" + arr1 [2] + "day ");
16 // we often use indexOf when searching strings. The corresponding method for searching regular expressions is search.
17 str2 = "the group violence in wansheng district cannot afford to hurt children in Chongqing! "; // The age is not certain. We cannot use indexOf to find its location.
18 re2 =/cannot afford to hurt /;
19 alert (str2.search (re2); // return the searched string start subscript 15
20 re3 =/cannot afford to hurt/g; // we add a global ID and find that it only matches the location where it appears for the first time.
21 // how do I perform global search? LastIndex
22 alert (str2.search (re3); // return the searched string start subscript 15
23 // if no match is found in the search method,-1 is returned.
24 // similar to the exec method, the match method of the String object is also used to match the String with the regular expression and return the result array.
25 // note the difference between Match and MatchCollection in. net, but it is similar here.
26 var str4 = "I deeply LOVE my country and people! ";
27 var re4 =/[A-Z] // match all uppercase letters
28 alert (str4.match (re4); // This is just or returns I
29 var re5 =/[A-Z]/g; // match all uppercase letters
30 alert (str4.match (re5); // Returns ["I", "L", "O", "Y"]
31 // extract words from strings
32 re =/\ B [a-z] \ B/I; // \ B indicates the word boundary
33 str = "one two three four ";
34 alert (str. match (re); // one, two, three, four
35 </script>
RegExp object instance attributes
Var re =/[a-z]/I;
Alert (re. source); // output the [a-z] string
// Note that direct alert (re) will output the regular expression along with the forward slash and sign, which is defined by the re. toString method.
Var re1 = new RegExp ("[a-z]", "I ");
Alert (re1.source); // outputs the [a-z] string
The instance of each RegExp object has the lastIndex attribute, which is the starting position of the next successful match in the searched string. The default value is-1.
Var re =/[a-z]/ig;
Var str = "Hello World! ";
Alert (re. test (str ));
Alert (re. lastIndex); // 1
Alert (re. test (str ));
Alert (re. lastIndex); // 2
Alert (re. test (str ));
Alert (re. lastIndex); // 3
Alert (re. test (str ));
Alert (re. lastIndex); // 4
Var re1 =/[a-z]/ig;
Var str1 = "Hello World! ";
Var arr=re1.exe c (str1 );
Alert (re1.lastIndex); // 1
Var arr=re1.exe c (str1 );
Alert (re1.lastIndex); // 2
The lastIndex attribute is modified by the exec and test methods of the RegExp object. and it is writable. if the matching fails (not matched later) or the lastIndex value is greater than the string length, execute exec and other methods to set the lastIndex to 0 (starting position ).
Static attributes of RegExp objects
Source, returns a copy of the text in regular expression mode. Read-only.
LastIndex returns the character position, which is the starting position of the next successful match in the searched string.
$1... $9, returns nine recently saved parts found during pattern matching. Read-only.
Input ($ _), returns the string to be searched according to the execution specification. Read-only.
LastMatch ($ &) returns the final matched characters in any regular expression search. Read-only.
LastParen ($ +), if any, returns the final child match in any regular expression search. Read-only.
LeftContext ($ ') returns the character from the starting position of the string to the position before the final match. Read-only.
RightContext ($ ') returns the character from the last matching position to the end of the string. Read-only.
// Input is the final matched string (the string passed to the test, exec method)
Varre =/[A-Z]/;
Var str = "Hello, World !!! ";
Var arr = re.exe c (str );
Alert (RegExp. input); // Hello, World !!!
Re.exe c ("asd"); // because tempstr does not match
Alert (RegExp. input); // It is still Hello, World !!!
Var re1 =/\ d /;
Var str1 = "spring 2012 ";
Var B = re1.test (str1 );
Alert (RegExp. input); // "spring 2012"
Var c = re1.test ("two birds on the tree make love every day! ")
Alert (RegExp. input); // still "2012 Spring"
// The Last matched character of lastMatch
Re =/[a-z]/g;
Str = "love ";
Re. test (str );
Alert (RegExp. lastMatch); // l
Re. test (str );
Alert (RegExp ["$ &"]); // o, $ & is the short name of lastMatch, but it is required because it is not a valid variable name ..
// The Last matched group of lastParen
Re =/[a-z] (\ d +)/gi;
Str = "Class1 Class2 Class3 ";
Re. test (str );
Alert (RegExp. lastParen); // 1
Re. test (str );
Alert (RegExp ["$ +"]); // 2
// LeftContext returns the character in the searched string from the starting position of the string to the position before the final match.
// RigthContext returns the character from the last matching position to the end of the string.
Re =/[A-Z]/g;
Str = "123ABC456 ";
Re. test (str); //
Alert (RegExp. leftContext); // 123
Alert (RegExp. rightContext); // BC456
Re. test (str); // B
Alert (RegExp ["$ '"]); // 123A
Alert (RegExp ["$ '"]); // C456
 

Author: Xie xiaoge
 

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.