JS Regular expression

Source: Internet
Author: User

Regular expression, there are wood people like me, learned several times but still very ignorant circle, learning when the old understand, learned to forget the light. Well, in fact, or practice is not enough, so-called temperature so that the new, can be a teacher, today with me to review this proud of the regular expression bar.

Why do we have regular expressions? In fact, because the computer is stupid (this is not what I said), such as [email protected], we look at is the mailbox, but the computer does not know Ah, so we have to use some computer know the language, to make good rules, tell it conforms to this rule is a mailbox, So that the computer can help us find the corresponding thing. So the regular is used to set rules, to complete our needs of some operations, such as login verification, search for the specified things and so on, said too much is superfluous, directly to the point.

define the Regular:

1 var re = new RegExp ("a");  The RegExp object. Parameters are the rules we want to make. There is a situation that must be used in this way, as mentioned below. 2 var re =/a/;   The shorthand method recommends the use of better performance  can not be empty or you think it is a comment,

Common methods of regular

1 Test (): Finds the content in the string that conforms to the regular, and returns False if the lookup returns TRUE.

Usage: Regular. Test (String)

Example: Judging whether it is a number

var str = ' 374829348791 '; var re =/\d/;  \d represents a non-numeric if (Re.test (str)) {   //returns True, which represents a non-number found in a string.    alert (' Not all numbers ');} else{    alert (' All numbers ');}

There are many symbols in regular expressions that represent different meanings that we can use to define different rules, such as the \d above, and the following:

\s: Space
\s: Non-whitespace
\d: Digital
\d: Non-digital
\w: Characters (Letters, numbers, underscores _)
\w: Non-character Example: whether there are characters that are not numbers

(The following will be based on examples, in turn, some of the commonly used characters, and finally summarized.) )

2 search (): Search for regular content in a string, search to return to the location of the occurrence (starting from 0, if the match is not just a letter, that will only return the position of the first letter), if the search failed to return-1

usage : String. Search (Regular)

finds the contents of a compound regular in a string. Ignore case: I--ignore(the default in regular is case-sensitive, if case insensitive, at the end of the regular ID i)

Example: Finding the letter B in a string with case-insensitive

var str = ' abcdef '; var re =/b/i;//var re = new RegExp (' B ', ' I '); You can also write alert (Str.search (re)); 1

3 match () searches the string for the contents of the compound rule, returns the content when the search succeeds, formats an array, and returns null if it fails.
Usage: string. Match (Regular)
Quantifier: + At least one occurrence of an indeterminate number of times (matching is the meaning of search lookup)
Global match: G--global(the default in regular, as long as the search for the contents of the compound rule will end the search)

Example: Find all numbers in the specified format, as found below 123,54,33,879
var str = ' haj123sdk54hask33dkhalsd879 '; var re =/\d+/g;   Matches at least one number at a time  and the global match  if it is not a global match, it will stop when the number 123 is found. It just pops up. 123. With a global match, the search is consistent from start to finish. If there is no plus, the result of the match is that 1,2,3,5,4,3,3,8,7,9 is not what we want, and with the plus sign, the number that matches each time is at least one. Alert (Str.match (re));   [123,54,33,879]

4 replace (): Finds a string that matches the regular character and replaces it with the corresponding string. Returns the content after the replacement.

Usage: string. Replace (regular, new string/callback function) (in the callback function, the first parameter refers to the character that each match succeeds)

|: or meaning.

Example: Sensitive word filtering, such as I love Beijing Tian ' an gate, the sun rises in Tiananmen Square. ------ I love *****,**** on the sun rise. That is, Beijing and Tiananmen become * numbers,

At first we might think of such a method:

var str = "I love Beijing Tian An men, the sun rises on Tiananmen Square." "; var re =/Beijing | Tiananmen/g;  find Beijing or Tiananmen global match var str2 = str.replace (Re, ' * '); alert (STR2)  //I love **,* on the Sun//This just turns the found into a *, and can not be a few words corresponding to a few *.

To implement a few words corresponding to several *, we can use the callback function to implement:

var str = "I love Beijing Tian An men, the sun rises on Tiananmen Square." "; var re =/Beijing | Tiananmen/g;  find Beijing or Tiananmen global match var str2 = str.replace (re,function (str) {              alert (str);///For testing: The first parameter of the function represents the regular character of each search, So the first time Str refers to the second time in Beijing str is Tiananmen Square the third time Str is Tiananmen Square            var result = ';            for (Var i=0;i<str.length;i++) {                result + = ' * ';            }                          return result; So search for a few words to return a few *         }); alert (STR2)  //I love *****,*** on the sun rise//The         whole process is to find Beijing, replaced by two *, find Tiananmen Square replaced by 3 *, Find Tiananmen Square replaced by 3 *.

Replace is a useful method that is often used.

The characters in the regular

():, parentheses, called a group character. is equivalent to the parentheses in mathematics. As follows:

var str = ' 2013-6-7 '; var re1 =/\d-+/g;  The global match number, the horizontal bar, the number of bars is at least 1, the matching result is:  3-6-var Re1 =/(\d-) +/g;  Globally matched numbers, bars, numbers and bars with a total quantity of at least 1   3-6-var re2  =/(\d+) (-)/g;  globally matches at least one number, matching a cross-bar match result: 2013-6-

At the same time, each parenthesized item in the regular is called the regular subkey. Children are very useful at some point, for example, we look at a chestnut.

Example: Making 2013-6-7 into 2013.6.7

var str = ' 2013-6-7 '; var re =/(\d+) (-)/g;str = Str.replace (re,function ($0,$1,$2) {           //replace () If there are children,
First parameter: $ $ (the overall result after a successful match 2013- 6-), //second parameter: $ $ (the first group to match success, here is \d , 6) ///Third parameter: $ ( The second group of successful matches, here is the--- ) return $ + '. Return to. 6. }); alert (str); 2013.6.7//the whole process is to use the children to replace 2013-6-2013. 6. final pop-up 2013.6.7

The match method also returns its own subkey, as follows:

var str = ' abc '; var re =/(a) (b) (c)/;alert (Str.match (re));  [Abc,a,b,c] (returns a matching result and each subkey  can get a collection of subkeys when match does not have G)

Add: Exec () method: Like the match method, searches for content that conforms to the rule and returns the content, formatted as an array.

Usage: Regular. exec (string);

Property: Input (representing the string to match)

Chestnuts: is not the case for a global match:

var teststr = "Now test001 test002";    var re =/test (\d+)/; Match only once      var r = "";    var r = re.exec (TESTSTR)  alert (R),//test001  001 Returns the matching result, and the child  alert (r.length);//2   Returns the length  of the content alert (r.input); Now test001 test002    represents a successful string   alert (r[0]) for each match;   test001     alert (r[1]);  001    represents the first subkey (\d+)   alert (r.index) in each matching success string;  4   

Global match: If it is a global match, you can find each match to the string, along with the subkey, through the while loop. Each match is followed by the last position to begin matching

var teststr = "Now test001 test002";    var re =/test (\d+)/g;     var r = "";   Match two times each match is followed by the last position to start the match, matching until the end of R is false, stopping matching matches to test001 test002  while (r = re.exec (teststr)) {    alert (r);// Returns the string that each match succeeds, and the subkey, respectively: Test001 001,test002  002    alert (r.input);//popup: Now   test001 test002 now    test001 test002      alert (r[0]);   A string representing the success of each match  pops up separately:  test001     test002    alert (r[1);  Represents the first subkey (\d+) in each matching success string,  respectively: 001   002    alert (r.index);   The position of the first character in each successful string match is popped: 4    alert (r.length);//popup: 2   2}

[]: Represents any one of a set, such as [ABC] as a whole represents a character that matches either a B C or a range, [0-9] The range must be small to large.

[^a] The whole represents a character: ^ written in [], the meaning of the exclusion

Example: matching HTML tags such as <div class= "B" >hahahah </div> Find tags <div class= "b" ></div>

var re =/<[^>]+>/g; Match the contents of at least one non-closing parenthesis in the middle of the opening parenthesis (because there are attributes in the label, etc.), and then match the closing parenthesis var re =/<[\w\w]+>/g; Matches the contents of at least one character or non-character in the middle of the opening parenthesis, and then matches the closing parenthesis
is to find the left parenthesis, then the middle can have at least one content, until the right parenthesis is found to represent a label.

Escape character

\s: Space
\s: Non-whitespace
\d: Digital
\d: Non-digital
\w: Characters (Letters, numbers, underscores _)
\w: Non-character
. (dot)--any character
\. : The real point
\b: Separate section (Start, end, space)
\b: A non-independent part

About the last two to see a chestnut:

var str = ' Onetwo '; var str2 = "one"; var re =/one\b/;  E after must be independent can be the start, space, or End alert (re.test (str)); Falsealert (Re.test (str2));//true  

Example: Write a function that gets a node with a class name:

We may have seen such a function before:

    function Getbyclass (parent,classname) {     if (parent.getelementsbyclassname) {                 return Parent.getelementsbyclassname (classname);    }    else{        var results = new Array ()//is used to store all the elements that are taken by the class box as        var elems = parent.getelementsbytagname ("*");        for (var i =0;i<elems.length;i++) {           if (elems[i].classname==classname) {              results.push (elems[i]);           }        }       return results;      

In fact, this is problematic, for example if it has two classes in a tag, or a class with the same name, such as <div class= "Box1 box1" >,<div class= "box1 box2> it can't get it. , we can use the regular to solve this problem.

function Getbyclass (parent,classname) {    if (parent.getelementsbyclassname) {        return Parent.getelementsbyclassname (classname);    } else{        var arr = [];        var Aele = parent.getelementsbytagname (' * ');                var re =/\bclassname\b/;  Can not write this, when the regular need to use parameters, must use the full name of the wording, shorthand method will be classname as a string to match.        var re = new RegExp (' \\b ' +classname+ ' \\b ');  When matching, the front of the classname must be a start or a space, followed by. The default match succeeds and stops, so even if there are duplicates, it will not match again. It is important to note that when a regular is declared in the full name, the        arguments are of the string type, so we need to ensure that the special characters are output within the string. The \b itself is a special character that cannot be exported in a string, so the backslash is escaped. For          (Var i=0;i<aele.length;i++) {            if (re.test (Aele[i].classname)) {                Arr.push (aele[i]);            }        }                return arr;    }            }

\a represents a repeating subkey, such as:

\1 the first subkey to repeat

\2 Second subkey to repeat

/(a) (b) (c) \1/-----match abca/(a) (b) (c) \2/------match ABCB

Example (frequently asked in an interview question): The maximum number of characters to find duplicates

Split (): A method in a string that transfers a string to an array.

Sort (): The sorting method in the array, sorted by Acall code.

Join (): A method in an array that converts an array to a string

varstr = ' ASSSSJDSSSKSSALSSSDKJSSSDSS '; vararr = Str.split (");//Convert a string to an arraystr = Arr.sort (). Join (');//sort first, so that the result will put the same characters together and then convert to a string        //alert (str); Aaddjjkklsssssssssssssssss         varValue = '; varindex = 0; varRe =/(\w) \1+/g;//Matches the character and repeats the character at least once. Str.replace (Re,function($0,$1){            //alert ($); Represents a successful result for each match: AA DD JJ KK L SSSSSSSSSSSSSSSSS             //alert ($); Represents the first subkey for each successful match, i.e. \w:a D J k L S                        if(index<$0.length) {//If index holds a value that is less than the length of $ A, do the followingindex = $0.length;//so the index has been saved at the maximum lengthValue = $ $;//value holds the most occurrences of this character            }        }); //alert (' Most characters: ' +value+ ', repeat number: ' +index '); S

Press CTRL + C to copy the code<textarea></textarea>Press CTRL + C to copy the code
quantifier : Indicates the number of occurrences

{n,m}: appears at least n times, up to M times

{N,}: at least n times

*: Any time equivalent to {0}

? : 0 or one time equivalent to {0,1}

+: Once or any time equivalent to {1}

{n}: exactly n times

Example: Judging is not QQ number

^: placed at the beginning of the regular position, the meaning of the beginning, notice/[^a]/and/^[a]/is not the same, the former is the meaning of exclusion, the latter is representative of the first.
$: The last position of the regular is the meaning of the end

     First of all want to QQ number rules       1 First position cannot be 0       2 must be 5-12 bits of the number       var ainput = document.getelementsbytagname (' input ');    var re =/^[1-9]\d{4,11}$/;    123456ABC to prevent such a situation, it is necessary to limit the last    //First is 0-9, followed by the 4-11-bit numeric type. Ainput[1].onclick = function () {    if (re.test (Ainput[0].value)) {        alert (' is QQ number ');    } else{        alert (' not QQ number ');}    ;    

Example: Remove front and back spaces (face questions often appear)

var str = '  Hello  '; alert (' (' +trim (str) + ') ');//To see the difference so the parentheses are added. (hello) function trim (str) {   var re =/^\s+|\s+$/g;//| represents or   \s represents a space  + at least one preceded by at least one space or at least one    space followed by a global match  return Str.replace (Re, ");//Replace space with empty}

Some of the commonly used form checksums

Match Chinese: [\U4E00-\U9FA5]//Chinese Acall code range beginning line End space: ^\s*|\s*$//The first line appears any space or the tail line appears any space (any means can also have no space) Email:^\[email protected][ a-z0-9]+ (\.[ a-z]+) {1,3}$        //start at least one character (\w letters, numbers, or underscores), then match @, followed by any letter or number, \. Represents the real point,. The character behind is at least one (A-Z), and this (for example,. com) The whole is the end of a subkey and can occur 1-3 times. Because some of the mailboxes are like this. cn.net. ([email protected] [email protected] [email protected]) URL: [a-za-z]+://[^\s]*/   http  /... Match any letter of the case, followed by//, followed by any character that is not a space zip code: [1-9]\d{5}  //The starting number cannot be 0, then 5 digital IDs: [1-9]\d{14}|[ 1-9]\d{17}| [1-9]\d{16}x

For convenience and no conflict, we can create our own space in JSON format, as follows:

/*

var re = {
Email:/^\[email protected][a-z0-9]+ (\.[ a-z]+) {1,3}$/,
Number:/\d+/
};

Re.email

*/

Regular basic knowledge points about so much, write some confusion, welcome to correct, if you want to reprint please indicate the source.

JS Regular expression

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.