Proficient in regular expressions in JavaScript mobile phone sorting recommendations

Source: Internet
Author: User
Tags control characters expression engine

The regular expression can be:
• Test a mode of a string. For example, you can test an input string to see if there is a phone number or a credit card number. This is called Data Validity verification.
• Replace text. You can use a regular expression in a document to identify a specific text, and then delete it all or replace it with another text.
• Extract a substring from the string based on the pattern match. It can be used to search for specific text in text or input fields.
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.
Create Regular Expression
Js Code
Copy codeThe Code is as follows:
Var re = new RegExp (); // RegExp is an object, same as Aarray
// But this does not have any effect. You need to pass the content of the regular expression as a string.
Re = new RegExp ("a"); // The simplest regular expression that matches the letter
Re = new RegExp ("a", "I"); // The second parameter indicates that the matching is case-insensitive.


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)
Js Code
Copy codeThe Code is as follows:
Var re = new RegExp ("a", "gi"); // match all a or

There is another way to declare a regular expression literally.
Js Code
Copy codeThe Code is as follows:
Var re =/a/gi;

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 the package <script type = "text/javascript" src = "http://www.javaeye.com/javascripts/tinymce/themes/advanced/langs/zh.js"> </script> <script type = "text/javascript" src = "http://www.javaeye.com/javascripts/tinymce/plugins/javaeye/langs/zh.js"> </script> An array containing the search result.
• Compile, which compiles regular expressions into internal formats for faster execution.
Attributes of a regular expression object
• 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 ($ _), which 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 characters in the searched string 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.
Some methods related to the regular expression of the String object
• Match: finds matching of one or more regular expressions.
• Replace: replace the substring that matches the regular expression.
• Search: searches for values that match regular expressions.
• Split: Splits a string into a string array.

Test how regular expressions work!
Copy codeThe Code is as follows:
// Test method, test string. true is returned when the mode is met; otherwise, false is returned.
Var re =/he/; // The simplest regular expression that matches the word he.
Var str = "he ";
Alert (re. test (str); // true
Str = "we ";
Alert (re. test (str); // false
Str = "HE ";
Alert (re. test (str); // false, in upper case. You can specify the I flag if both cases are required. (I indicates ignoreCase or case-insensitive)
Re =/he/I;
Alert (re. test (str); // true
Str = "Certainly! He loves her! ";
Alert (re. test (str); // true, as long as it contains he (HE), it will match. If it is only he or HE and cannot contain other characters, you can use ^ and $
Re =/^ he/I; // escape character (^) indicates the starting position of the character
Alert (re. test (str); // false, because he is not at the beginning of str
Str = "He is a good boy! ";
Alert (re. test (str); // true, He is the starting position of the character, and $
Re =/^ he $/I; // $ indicates the character ending position
Alert (re. test (str); // false
Str = "He ";
Alert (re. test (str); // true
// Of course, we cannot find how powerful the regular expression is, because we can use = or indexOf in the above example.
Re =/\ s/; // \ s matches any blank characters, including spaces, tabs, and page breaks.
Str = "user Name"; // the user Name contains spaces
Alert (re. test (str); // true
Str = "user Name"; // the user Name contains tabs.
Alert (re. test (str); // true
Re =/^ [a-z]/I; // [] matches any character in the specified range. English letters are matched here, which are case insensitive.
Str = "variableName"; // The variable name must start with a letter
Alert (re. test (str); // true
Str = "123abc ";
Alert (re. test (str); // false

Of course, it is not enough to know whether the string matches the pattern. We also need to know which characters match the pattern.
Copy codeThe Code is as follows:
Var osVersion = "Ubuntu 8"; // The value 8 indicates the system master version.
Var re =/^ [a-z] + \ s + \ d + $/I; // + indicates that the character must appear at least once, and \ s indicates a blank character, \ d indicates a number
Alert (re. test (osVersion); // true, but we want to know the main version number.
// The exec method returns an array. The first element of the array is the complete Matching content.
Re =/^ [a-z] + \ s + \ d + $/I;
Arr = re.exe c (osVersion );
Alert (arr [0]); // output the complete osVersion because the entire string matches the re
// I only need to retrieve numbers
Re =/\ d + /;
Var arr = re.exe c (osVersion );
Alert (arr [0]); // 8

For more complex usage, use sub-matching
Copy codeThe Code is as follows:
// The array 1st to nelement returned by exec contains any child match in the match.
Re =/^ [a-z] + \ s + (\ d +) $/I; // use () to create a submatch
Arr implements re.exe c (osVersion );
Alert (arr [0]); // The entire osVersion, that is, the complete match of the Regular Expression
Alert (arr [1]); // 8, the first child matches, and the main version can also be retrieved in this way.
Alert (arr. length); // 2
OsVersion = "Ubuntu 8.10"; // obtain the main version and minor version
Re =/^ [a-z] + \ s + (\ d + )\. (\ d +) $/I ;//. is one of the metacharacters of a regular expression. to use its literal meaning, it must be escaped.
Arr = re.exe c (osVersion );
Alert (arr [0]); // complete osVersion
Alert (arr [1]); // 8
Alert (arr [2]); // 10

Note: When the string does not match re, the exec method returns null.
Some methods of String objects related to regular expressions
// Replace method, used to replace the string var str = "some money"; alert (str. replace ("some", "much"); // The first parameter of much money // replace can be the regular expression var re =/\ s /; // blank character alert (str. replace (re, "%"); // some % money // when you do not know how many white spaces are in the string, regular Expressions are extremely convenient for str = "some \ tsome \ t \ f"; re =/\ s +/; alert (str. replace (re, "#"); // but this will only replace a bunch of blank characters that appear for the first time. // because a regular expression can only match once, \ s + matches the first space and then exits re =/\ s +/g; // g, global flag. The regular expression will match the entire string alert (str. replace (re, "@"); // som E @ some @ // another similarity is split var str = "a-bd-c"; var arr = str. split ("-"); // Returns ["a", "bd", "c"] // If str is input by the user, he may input a-bd-c or a bd c or a_bd_c, but it won't be abdc (so that he is wrong) str = "a_db-c "; // The user adds the separator s re =/[^ a-z]/I in his favorite way; // we have mentioned ^ to indicate that the character starts, however, in [], it indicates a negative character set // It matches any character that is not within the specified range. Here, it matches all characters except letters, arr = str. split (re); // still Returns ["a", "bd", "c"]; // indexOf, search str = "My age is 18. golden age! "; // The age is not certain. We cannot use indexOf to find its location re =/\ d +/; alert (str. search (re); // returns the searched string starting with subscript 10 // note, because the search itself is the first occurrence and returns immediately, therefore, you do not need to use the g flag when searching. // although the code below does not make an error, the g flag is redundant re =/\ d +/g; alert (str. search (re); // still 10
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Note: If the search method does not find a match,-1 is returned.
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.
Copy codeThe Code is as follows:
Var str = "My name is CJ. Hello everyone! ";
Var re =/[A-Z] // match all uppercase letters
Var arr = str. match (re); // returns an array
Alert (arr); // The array contains only one M, because we do not use global match.
Re =/[A-Z]/g;
Arr = str. match (re );
Alert (arr); // M, C, J, H
// Extract words from strings
Re =/\ B [a-z] \ B/I; // \ B indicates the word boundary
Str = "one two three four ";
Alert (str. match (re); // one, two, three, four

RegExp object instance attributes
Js Code
Copy codeThe Code is as follows:
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.

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. The lastIndex attribute is modified by the exec and test methods of the RegExp object. It is writable.
Copy codeThe Code is as follows:
Varre =/[A-Z]/;
// After the exec method is executed, the lastIndex attribute of re is modified,
Var str = "Hello, World !!! ";
Var arr = re.exe c (str );
Alert (re. lastIndex); // 0 because the global flag is not set
Re =/[A-Z]/g;
Arr = re.exe c (str );
Alert (re. lastIndex); // 1
Arr = re.exe c (str );
Alert (re. lastIndex); // 7

If the match fails (no match is found later) or the lastIndex value is greater than the string length, execute exec and other methods to set the lastIndex to 0 (start position)
Copy codeThe Code is as follows:
Varre =/[A-Z]/;
Var str = "Hello, World !!! ";
Re. lastindexes = 120;
Var arr = re.exe c (str );
Alert (re. lastIndex); // 0

Static attributes of RegExp objects
Copy codeThe Code is as follows:
// Input the final string used for matching (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 ("tempstr ");
Alert (RegExp. input); // It is still Hello, World !!!, Because tempstr does not match
// The Last matched character of lastMatch
Re =/[a-z]/g;
Str = "hi ";
Re. test (str );
Alert (RegExp. lastMatch); // h
Re. test (str );
Alert (RegExp ["$ &"]); // I, $ & 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 );
Alert (RegExp ["$ '"]); // 123A
Alert (RegExp ["$ '"]); // C456

The multiline attribute returns whether the regular expression uses the multiline mode. This attribute is not applicable to a regular expression instance, but to all regular expressions. This attribute is writable. (IE and Opera do not support this attribute)
Copy codeThe Code is as follows:
Alert (RegExp. multiline );
// Because IE and Opera do not support this attribute, it is best to specify it separately.
Var re =/\ w +/m;
Alert (re. multiline );
Alert (RegExp ["$ *"]); // The static attribute of the RegExp object does not change because the m flag is specified for a RegExp object instance.
RegExp. multiline = true; // This will enable the multiline matching mode for all regular expression instances
Alert (RegExp. multiline );

Precautions for using metacharacters: metacharacters are part of regular expressions. to match the regular expression itself, escape these metacharacters. Below are all metacharacters used by regular expressions.
([{\ ^ $ | )? * +.
Copy codeThe Code is as follows:
Var str = "? ";
Var re = /? /;
Alert (re. test (str); // error because? It is a metacharacter and must be escaped.
Re = /\? /;
Alert (re. test (str); // true

Notes for using RegExp constructor and using regular expressions to create regular expressions literally
Copy codeThe Code is as follows:
Var str = "\? ";
Alert (str); // only outputs?
Var re = /\? // Will match?
Alert (re. test (str); // true
Re = new RegExp ("\? "); // Error, because this is equivalent to re = /\? /
Re = new RegExp ("\\? "); // Correct, will match?
Alert (re. test (str); // true

Since double escaping is so unfriendly, we still use the regular expression to declare it literally.
How do I use special characters in regular expressions?
Copy codeThe Code is as follows:
// Use hexadecimal notation to represent special characters in ASCII format
Var re =/^ \ x43 \ x4A $/; // match CJ
Alert (re. test ("CJ"); // true
// You can also use the octal method.
Re =/^ \ 103 \ 112 $/; // match CJ
Alert (re. test ("CJ"); // true
// You can also use Unicode encoding.
Re =/^ \ u0043 \ u004A $/; // Unicode must begin with u, followed by the four-digit hexadecimal representation of character encoding
Alert (re. test ("CJ "));

In addition, there are some other predefined special characters, as shown in the following table:
Character Description
\ N linefeed
\ R carriage return
\ T Tab
\ F Tab character)
\ Control characters corresponding to cX and X
\ B BackSpace)
\ V vertical Tab
\ 0 null character ("")
Character class ---> simple class, reverse class, range class, combination class, pre-defined class
Copy codeThe Code is as follows:
// Simple class
Var re =/[abc123]/; // match one of the six characters abc123
// Negative
Re =/[^ abc]/; // match a character other than abc
// Range
Re =/[a-B]/; // Match 26 lowercase letters a-B
Re =/[^ 0-9]/; // match a character except 0-9 10 Characters
// Combination
Re =/[a-b0-9A-Z _]/; // match letters, numbers, and underscores

The following are pre-defined classes in regular expressions.

Code is equivalent to matching
In IE, [^ \ n] and other [^ \ n \ r] match any character except the linefeed.
\ D [0-9] matches numbers
\ D [^ 0-9] matches non-numeric characters
\ S [\ n \ r \ t \ f \ x0B] matches a blank character
\ S [^ \ n \ r \ t \ f \ x0B] matches a non-blank character
\ W [a-zA-Z0-9 _] match letters, numbers and underscores
\ W [^ a-zA-Z0-9 _] match characters other than letters, numbers, and underscores

Quantifiers (The following table quantifiers are greedy quantifiers when they appear)
Code Description
* 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} n is a non-negative integer. Match 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 *'.
Both {n, m} m and n are non-negative integers, where n <= m. Match at least n times and at most m times. 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.

Greedy quantifiers and inert quantifiers
• When matching with greedy quantifiers, it will first treat the entire string as a match. If it matches, it will exit. If it does not match, it will take the last character for matching, if no match exists, the last character is intercepted until a match exists. Until now, all the quantifiers we encounter are greedy quantifiers.
• When matching with greedy quantifiers, it first treats the first character as a match. If it succeeds, it exits. If it fails, the first two characters of the test are added accordingly, until an appropriate match occurs.
The inert quantizer only adds "? ", For example," a + "is greedy," a +? "It is inert.
Copy codeThe Code is as follows:
Var str = "abc ";
Var re =/\ w +/; // match abc
Re =/\ w +? // Match

Multiline Mode
Copy codeThe Code is as follows:
Var re =/[a-z] $ /;
Var str = "AB \ ncdef ";
Alert (str. replace (re, "#"); // AB \ ncde #
Re =/[a-z] $/m;
Alert (str. replace (re, "#"); // a # \ ncde #

Group and non-capturing Group
Copy codeThe Code is as follows:
Re =/abc {2}/; // match abcc
Re =/(abc) {2}/; // match abcabc
// The above groups are capturing groups.
Str = "abcabc ###";
Arr = re.exe c (str );
Alert (arr [1]); // abc
// Non-capturing group (? :)
Re = /(? : Abc) {2 }/;
Arr = re.exe c (str );
Alert (arr [1]); // undefined

Candidate (that is, "or ")
Copy codeThe Code is as follows:
Re =/^ a | bc $/; // match the at the starting position or the bc at the ending position
Str = "add ";
Alert (re. test (str); // true
Re =/^ (a | bc) $/; // match a or bc
Str = "bc ";
Alert (re. test (str); // true

After the regular expressions containing groups are tested, matched, and searched, each group is placed in a special place for future use. These are the special values in the group, this is called reverse reference.
Copy codeThe Code is as follows:
Var re =/(? (B? (C ?))) /;
/* The above regular expression will generate three groups in sequence
(? (B? (C ?))) Outermost
(B? (C ?))
(C ?) */
Str = "ABC ";
Re. test (str); // The Reverse reference is stored in the static attribute $1-$9 of the RegExp object.
Alert (RegExp. $1 + "\ n" + RegExp. $2 + "\ n" + RegExp. $3 );
// Reverse reference can also be used in regular expressions in the form of \ 1, \ 2...
Re =/\ d + (\ D) \ d + \ 1 \ d + /;
Str = "maid ";
Alert (re. test (str); // true
Str = "2008-4_3 ";
Alert (re. test (str); // false

Using Reverse references requires that the characters at certain positions in the string must be the same. In addition, special character sequences can be used in replace methods to represent reverse references.
Js Code
Copy codeThe Code is as follows:
Re =/(\ d) \ s (\ d )/;
Str = "1234 5678 ";
Alert (str. replace (re, "$2 $1"); // in this example, $1 indicates that the first group is 1234, and $2 indicates 5678.

Others --> forward lookup is used to capture characters that appear before a specific character. It is captured only when the character is followed by a specific character. There is a negative forward direction corresponding to the forward direction. It matches only when the character is not followed by a specific character. When performing operations such as forward-looking and negative-looking-ahead, the Regular Expression Engine will pay attention to the part behind the string, but does not move the index
Copy codeThe Code is as follows:
// Forward looking
Re =/([a-z] + (? = \ D)/I;
// We need to match the word followed by a number, and then return the word instead of a number.
Str = "abc every1 abc ";
Alert (re. test (str); // true
Alert (RegExp. $1); // every
Alert (re. lastIndex); // the advantage of using forward-looking is that forward-looking content (? = \ D) is not considered as a match. The next match will start from it.
// Negative forward view (?!)
Re =/([a-z] (?! \ D)/; I
// Match letters that do not contain digits and do not return (?! \ D) Content
Str = "abc1 one ";
Alert (re. test (str ));
Alert (RegExp. $1); // one

Construct a regular expression to verify the validity of the email address. Email Address validity requirements (defined in this way): the user name can only contain letters, numbers, and underscores (_). The user name can contain at least one digit and a maximum of 25 digits. the user name is followed by @, followed by the domain name, the domain name must only contain letters, numbers, and minus signs (-). It cannot start or end with a minus sign, followed by a domain name suffix (multiple domain names may exist ), the domain name suffix must contain 2-4 English letters
Copy codeThe Code is as follows:
Var re =/^ \ w {1, 15 }(? :@(?! -))(? :(? : [A-z0-9-] *) (? : [A-z0-9] (?! -))(? :\.(?! -) + [A-z] {2, 4} $ /;

It seems that some information is missing. For example, the second parameter of the replace method is used as a function.
The last time I saw an interview question in a blog, I read the strings in a text file and counted the number of times "9" appeared, writing a file in JS is also very easy (it does not contain the code for opening the file. You can use ActiveXObject and other objects provided by browsers to open the file)
Copy codeThe Code is as follows:
Var str = "ADF9DF9DF9", // the string in the text file;
Re =/9/gi, // match 9
Counter = 0; // counter
Str. replace (re, function (){
Counter ++; // each time a match occurs, the function is executed. The return value of the function is used to replace the original value.
Return "#";
});
// Finally, str is changed to ADF # DF #"

As for the parameters received by the function passed to the replace method, continue downstairs.
The question you asked:
$ & Is the short name of lastMatch
Also, I don't know why your regular expression does not escape {} two special characters.

When talking about JS compatibility, the compile method of the RegExp object instance is mentioned here.
The compile method is used to compile regular expressions into internal formats for faster execution.

Js Code
Copy codeThe Code is as follows:
Var re = new RegExp ();
Re. compile ("[0-9] \ n"); // note that you must escape the slash multiple times. The compile method returns the re object (Compiled)


However, after testing, in browsers like Safari and Chrome, the compile method always returns undefined and is unavailable.

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.