Regular Expressions common usage totals _ regular expressions

Source: Internet
Author: User
Tags html tags numeric

Before I read the text, we introduce the basic concept of regular expression :

Regular expressions, also known as formal representations, general representations. (English: Regular Expression, often abbreviated as regex, RegExp, or re) in code, a concept of computer science. A regular expression uses a single string to describe and match a sequence of rules that conform to a certain syntactic rule. In many text editors, regular expressions are often used to retrieve and replace text that conforms to a pattern.

Regular expression, there are some people like me, learned several times but still very Meng Quan Circle, learn when old understand, finished forgetting. Well, in fact, or practice is not enough, the so-called warm so know new, can be a teacher, today I will review this proud of the regular expression of it.

Why should there be regular expressions? It's because the computer is stupid (that's not what I said), For example, 123456@qq.com, we look at is the mailbox, but the computer does not know Ah, so we need to use some computer knowledge of the language, to make good rules, tell it conforms to this rule is a mailbox, so the computer can help us find the corresponding things. So the regular is used to set rules, to complete some of the operations we need, such as login verification, search for the specified things, and so on, said too much is superfluous, directly to the point of view.

Define Regular:

1 var re = new RegExp ("a"); RegExp object. Parameters are the rules we want to make. There is a situation that must be used in this way, as mentioned below.

Common methods of regular use

1 Test (): Find the content in the string that matches the regular, and return False if found to return true.

Usage: Regular. Test (String)

Example: Judge whether it is a number

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

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

\s: Space
\s: Non-space
\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 the example, followed by some commonly used characters, and finally make a summary.) )

2 Search (): The search to return the position (starting from 0, if not just a letter, which will only return the position of the first letter), if the search fails, returns-1 if it matches the regular content.

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 not case-sensitive, at the end of the regular sign i)

Example: Find the letter B in the string and do not case sensitive

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

3 match () searches for the contents of the compound rule in the string, and the search succeeds returns the content, the format is array, and the failure returns NULL.

Usage: string. Match (Regular)

Quantifier: + At least one occurrence of the number of uncertainties (matching is the meaning of search lookup)

Global match: G--global (default in regular, search is done as soon as the content of the composite rule is searched)

Example: Find all the numbers in the specified format, like the following find 123,54,33,879

var str = ' haj123sdk54hask33dkhalsd879 ';

var re =/\d+/g; Matches at least one digit at a time and if the global match is not a global match, when the number 123 is found, it stops. will only pop 123. Plus the global match, you'll always search for the rules from start to finish. If there is no plus sign, the result of the match is 1,2,3,5,4,3,3,879 is not what we want, with the plus sign, each matching number is at least one.

Alert (Str.match (re)); [123,54,33,879]

4 replace (): Find a regular string, replace it with the corresponding string. Returns the replaced content.

Usage: string. Replace (regular, new string/callback function) (in the callback function, the first parameter refers to the character that matches every success)

| : or meaning.

Example: Sensitive word filter, for example I love Beijing Tian an gate, Tiananmen Square on the sun rise. ------I love *****,**** on the sun rise. That Beijing and Tiananmen become *,

At first we might think of a way:

var str = "I love Beijing Tian ' an gate, Tian ' anmen sun rising." ";
var re =/Beijing | Tiananmen Square/g; Find Beijing or Tiananmen Global matching
var str2 = str.replace (Re, ' * '); 
Alert (STR2)//I love the **,* on the Sun 
//This just turns the found into a *, and not a few words on the corresponding several *.

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

var str = "I love Beijing Tian ' an gate, Tian ' anmen sun rising." ";
var re =/Beijing | Tiananmen Square/g; Find Beijing or Tiananmen. Global matching
var str2 = str.replace (re,function (str) {
alert (str);//To test: the first parameter of a function represents the regular character of each search. So the first time Str refers to the second STR in Beijing is Tiananmen Square, the third Str is the Tiananmen Square
var result = ';
for (Var i=0;i<str.length;i++) {Result
= ' * ';
} 
return result; So the search to a few words to return a few * 
});
Alert (STR2)//I love *****,*** on the Rising Sun

The whole process is to find Beijing, replaced by two *, to find Tiananmen replaced by 3 *, to find the Tiananmen Square replaced by 3 *.

Replace is a useful method that is often used.

Characters in regular

(): A parenthesis, called a grouping character. is equivalent to the parentheses inside the math. As follows:

var str = ' 2013-6-7 ';
var re1 =/\d-+/g; Global matching number, horizontal bar, horizontal bar number of at least 1, match the result is: 3-6-
var re1 =/(\d-) +/g;//Global match number, horizontal bar, number and horizontal bar total number of at least 1 3-6-
var Re2 =/(\d+) (-)/g; Global match 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 useful at some point, such as when we look at a chestnut.

Example: Let 2013-6-7 turn into 2013.6.7

var str = ' 2013-6-7 ';
var re =/(\d+) (-)/g;
str = str.replace (re,function ($0,$1,$2) {
//replace () If there is a subkey,//The first argument: $ (the overall result of the match succeeds 2013-6-),
//The second parameter: $ ( Match the first grouping of successes, which refers to the \d 2013, 6
///Third argument: $ (the second group that matches the success, which refers to--) 
return + '. ';///2013, respectively. 6.
});
alert (str); 2013.6.7
//The whole process is to use the subkey to replace 2013-6-respectively into 2013. 6. Final pop-up 2013.6.7

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

var str = ' abc ';
var re =/(a) (b) (c)/;
Alert (Str.match (re)); [Abc,a,b,c] (returns the match result and each subkey can get a set of subkeys when match is not g)

[]: Represents any one of a set, for example, [ABC] the whole represents one character match any of a B c, or it can be a range, [0-9] The range must be small to large.

[^a] The whole represents a character: ^ written in [] it means 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-right parenthesis in the middle of the opening parenthesis (because there are some things in the tag), and then match the closing parenthesis
var re =/<[\w\w]+>/g; Match the contents of at least one character or non character in the middle of the opening parenthesis, and then match the closing parenthesis//is to find the opening parenthesis, and then the middle can have at least one content, until the closing bracket is found to represent a label.

Escape character

\s: Space
\s: Non-space
\d: Digital
\d: Non-digital
\w: Characters (Letters, numbers, underscores _)
\w: Non-character
. (point)--any character
\. : The real point
\b: The independent part (start, end, space)
\b: Non-independent parts

For the last two, look at a chestnut:

var str = ' Onetwo ';
var str2 = "One Two";
var re =/one\b/; E back must be independent can be start, space, or End
alert (Re.test (str));//false
alert (re.test (STR2));//true

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

We might have seen a function like this before:

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

In fact, this is a problem, such as it if there are two classes in a tag, or a class with the same name, such as <div class= "Box1 box1 >,<div class=" Box1 box2> it will not be able to obtain , we can solve this problem with regular.

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 need to use the parameters, we must use the full name of the way, shorthand will be classname as a string to match.
var re = new RegExp (' \\b ' +classname+ ' \\b ');//When matched, the classname must be preceded by a start or a space, followed by a. The default match is stopped successfully, so even if there are duplicates, it will not match.
//It should be noted that when the full name method declares the regular, the argument is of the string type, so we need to ensure that these special characters can also be output within the string. The \b itself is a special character that cannot be output in a string, so it is possible to add a backslash escape. For 
(Var i=0;i<aele.length;i++) {
if (re.test (Aele[i].classname)) {
Arr.push (aele[i]);
}
Return
arr
} 
}

\a represents a duplicate of a subkey, such as:

\1 the first subkey that repeats

\2 Duplicate Second subkey

/(a) (b) (c) \1/-----matching ABCA
/(a) (b) (c) \2/------matching ABCB

Examples (frequently asked in interview questions): Find the number of characters that have duplicates

Split (): A method in a string that converts a string to a group.

Sort (): The sorting method in an 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 a string to an array of
str = Arr.sort (). Join (');///First sort, so the result will put the same characters together and then convert to string
//alert (str); Aaddjjkklsssssssssssssssss
var value = ';
var index = 0; 
var re =/(\w) \1+/g; Matches a character, and repeats the character at least once.
str.replace (re,function ($0,$1) { 
//alert ($); represents the successful result of each match: AA DD JJ KK L SSSSSSSSSSSSSSSSS
//alert ($); Represents the first subkey for each match success, that is, \w:a D J k L S
if (index<$0.length) {//If the value stored by index is less than the length of $ $, the following operation
index = $. Length So the index is kept at the maximum length
value = $//value Saves the most occurrence of this character
}
}; 
Alert (' Most characters: ' +value+ ', repeat the number of times: ' +index '); s 17  

Quantifiers: Represents the number of occurrences

{n,m}: At least n times, up to M times

{N,}: at least n times

*: Any time equal to {0,}

? : 0 times or once equivalent to {0,1}

+: Once or any time is equal to {1,}

{n}: just n times

Example: Judge is not QQ number

^: At the very beginning of the regular, on behalf of the beginning of the meaning, pay attention to/[^a]/and/^[a]/is not the same, the former is the meaning of exclusion, the latter is to represent the first place.

$: The last position of the regular is the meaning of the end

First think of the QQ number rules

1 first, not 0.

2 must be a 5-12-digit number

var ainput = document.getelementsbytagname (' input ');
var re =/^[1-9]\d{4,11}$/;
123456ABC in order to prevent such a situation, you must limit the last
//First is 0-9, followed by the 4-11-bit number type.
Ainput[1].onclick = function () {
if (re.test (Ainput[0].value)) {
alert (' QQ number ');
} else{
alert (' not QQ number ');
}
;

Example: Remove before and after space (face test often appear)

var str = ' Hello ';
Alert (' (' +trim (str) + ') ');//parentheses to see the difference. (Hello)
function Trim (str) {
var re =/^\s+|\s+$/g//| representative or \s represents a space + at least one front has at least one space or at least one trailing space followed by a global match return
Str.replace ( Re, ""); Replace the space with empty

Some of the commonly used form checksums

Match Chinese: [\U4E00-\U9FA5]//The range of Chinese Acall code

Beginning line Trailing space: ^\s*|\s*$//The first line appears any space or the end line appears any space (any representation can also have no spaces)

email:^\w+@[a-z0-9]+ (\.[ a-z]+) {1,3}$
Starting at least one character (\w letter, number or underscore), and then match @, followed by any letter or number, \. Represents the real point,. A character (A-Z) followed by at least one, and this (e.g.. com) total ends with a subkey, which can occur 1-3 times. Because some mailboxes are like this. cn.net. (xxxx. @qq. com

Xxxx.@163.com xxxx.@16.cn.net)

URL: [a-za-z]+://[^\s]* http://...

Matches any letter that is case-insensitive, followed by//, followed by any character that is not a space

ZIP/Postal code: [1-9]\d{5}//Starting number cannot be 0, then 5 digits

ID Card: [1-9]\d{14}| [1-9]\d{17}| [1-9]\d{16}x

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


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

The above is a small set to introduce the regular expression of common usage summary, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.