Summary of common regular usage

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:

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 non-numeric if (Re.test (str)) {   //Returns True, representing a non-digit found in the 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,879 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 //I love **,* on the Sun up //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); Used to test: the first parameter of the function represents each search to match the regular character, so the first STR refers to the second STR is the Tiananmen Square third Str is the Tiananmen Square            var result = ';            for (Var i=0;i<str.length;i++) {                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;  //Global match number, horizontal bar, the number of bars is at least 1, the matching result is:  3-6-var re1 =/(\d-) +/g;  //Global match number, horizontal bar, number and the overall number of bars is at least 1   3-6-var Re2  =/(\d+) (-)/g;  //  global match at least one number to match 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 successfully, this is \d , 6) // The third parameter: $ $ (the second grouping 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 match result and each subkey  can get a collection of subkeys when match does not have a G)

[]: 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>

//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 //Match the contents of at least one character or non-character in the middle of the opening parenthesis and match 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 start, space, or end //falsealert (re.test (STR2));

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 ();  Used to store all the elements of the class box that were taken        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.         //Match, 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.                 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

 var str = ' Assssjdssskssalsssdkjsssdss '; var arr = Str.split ('); //converts the string to an array  str = Arr.sort (). Join ('); //is sorted first so that the same characters are put together and then converted to a string   //alert (str); Aaddjjkklsssssssssssssssss  var value = '; var index = 0;  var re =/(\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, that is, \w:a D J k L S  if (index<$0.length) {//if index holds a value less than  The following actions are performed  index = $0.length;  //such that index has been saved at the maximum length  value = $ $; //value holds the most occurrences of the character }});  Alert (' Most characters: ' +value+ ', repeat number: ' +index '); //S- 
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 want the QQ number of the 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, the last    //first number must be limited to 0-9, followed by a 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) + ') ');  In order to see the difference so add the parentheses. (Hello) function Trim (str) {   //| 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  // Replace the space with empty }

Some of the commonly used form checksums

//The range of Chinese Acall code //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]*    //Match any letter of the case, followed by//, The following is any character postal code that is not a space : [1-9]\d{5}  //start number cannot be 0, then 5 digital ID: [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 point is probably so much, write a little disorderly, welcome correction

Summary of common regular usage

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.