JS Regular Induction Summary

Source: Internet
Author: User

Common methods of regular

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

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 )

// determine if it is a number

var str = ' 374829348791 ';

var re =/\d/; \d represents non-digital

if (Re.test (str)) {//returns True, representing a non-number found in the string.

Alert (' Not all numbers ');

}else{

Alert (' all numbers ');

}

2 search () : In the string search matches the regular content, the search will 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)

Find letter B in string and case insensitive

var str = ' abcdef ';
var re =/b/i;
var re = new RegExp (' B ', ' I '); Can also be written like this
Alert (Str.search (re)); 1

3 match () Searches for the contents of a compound rule in a string, returns content in a successful search, 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)

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 (): to find a regular string, replace 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,

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
This just turns the found into a *, and cannot 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 in Beijing is the third time Str is Tiananmen Square
var result = ';
for (Var i=0;i<str.length;i++) {
Result + = ' * ';
});
The whole process is to find Beijing, replaced by two *, find Tiananmen Square replaced by 3 *, find Tiananmen Square replaced by 3 *.

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, matching result is: 3-6-

var Re1 =/(\d-) +/g; Globally matched numbers, bars, numbers and bars with a total number 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) {

If there are children in replace (),
First parameter: $ $ (the overall result after a successful match 2013-6-),

The second parameter: $ $ (the first group to match successfully, this refers to \d 2013, 6)

The third parameter: $ $ (The second group of successful matches, here is the---)

return + '. ';   Returns 2013, respectively. 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

Match The 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)

Chestnut: Not a global match case:

Varteststr = "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 test001test002 represents a successful string for each match

Alert (r[0]); test001

Alert (r[1]); 001 represents the first subkey in the success string for each match (\d+)

alert (R.index); 4 The position of the first character in each successful string match

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

Varteststr = "Now test001 test002";

var re =/test (\d+)/g;

var r = "";

Match two times each match is followed by the last position to start matching, matching until the end of R is false, stop matching match to test001test002

while (R =re.exec (TESTSTR)) {

Alert (R);//returns the string that each match succeeds, and the subkey, respectively, pops up: test001 001,test002 002

alert (r.input); Pop-up: now test001 test002 now test001 test002

Alert (r[0]); The string that represents 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); Each match succeeds in the position of the first character in the string, respectively, popup: 4 12

alert (r.length); Pop-up: 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: Match HTML tags like <divclass= "b" >hahahah </div> find tags <divclass= "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 = "Onetwo";

var re =/one\b/; E after 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 may have seen such a function before:

Functiongetbyclass (parent,classname) {

if (parent.getelementsbyclassname) {

Returnparent.getelementsbyclassname (classname);

}

else{

var results = NewArray ();//used to store all the elements that were taken by the class box

var elems =parent.getelementsbytagname ("*");

for (Var i=0;i<elems.length;i++) {

if (elems[i].classname==classname) {

Results.push (Elems[i]);

}

}

Returnresults;

}

}

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= "Box1box1" >,<div class= "box1 box2> it can't get it. , we can use the regular to solve this problem.

Functiongetbyclass (parent,classname) {

if (parent.getelementsbyclassname) {

Returnparent.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 you declare a regular in the full name, the arguments are of the string type, so we need to ensure that these special characters can be 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 A subkey that represents a repetition, 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 ("); Convert a string to an array

Str= Arr.sort (). Join ('); Sort first, so that the result will put the same characters together and then convert 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 KKL sssssssssssssssss

Alert ($); Represents the first subkey for each successful match, i.e. \w:a D J k L S

  

if (index<$0.length) {//If the index holds a value less than the length of $ A, do the following

index = $0.length; So the index has been saved at the maximum length

Value = $ $; Value holds the most occurrences of this character

}

});

Alert (' Most characters: ' +value+ ', repeat number: ' +index '); s 17

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 think of the QQ number of the rules

1 first, not 0.

2 must be a number of 5-12 digits

var ainput = document.getelementsbytagname (' input ');

var re =/^[1-9]\d{4,11}$/;

123456ABC to prevent such a situation, you must limit the last

The first is 0-9, followed by a 4-11-bit numeric type.

ainput[1].onclick= function () {

if (Re.test (Ainput[0].value)) {

Alert (' 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 front has at least one space or at least one space behind it and a global match

Return Str.replace (Re, '); Replace the space with empty

}

Some of the commonly used form checksums

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

Line at the beginning of the end of the 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), and 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) whole is the end of a subkey, which 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 that is not case, followed by//, any character that is not a space followed by

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

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 points about so much, write some confusion, welcome to correct, if you want to reprint please indicate the source.

JS Regular Induction Summary

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.