Teach you easy to remember JS Regular expression _ regular expression

Source: Internet
Author: User
Tags html tags numeric trim

Objective

A regular expression (regular expression) describes a pattern of string matching that can be used to check whether a string contains a seed string, replaces a matching substring, or extracts a substring from a string that matches a condition. A regular expression is a literal pattern composed of ordinary characters, such as characters A through z, and special characters, called "metacharacters". The pattern describes one or more strings to match when searching for text. A regular expression is used as a template to match a character pattern with the string being searched for.

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 to understand 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:

Copy Code code as follows:

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.
var re =/a/; Shorthand method recommended to use better performance can not be empty or thought to be a note,

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

Copy Code code as follows:

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,8,7,9 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:

Copy Code code as follows:

var str = "I love Beijing Tian ' an gate, Tian ' anmen sun rising." ";
var re =/Beijing | Tiananmen Square/g; Find a Beijing or Tiananmen global match
var str2 = str.replace (re,function (str) {
alert (str); Used to test: the first argument of the function represents the regular character of each search, so the first STR refers to Beijing for the second time Str is Tiananmen and the third Str is Tiananmen.
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:

Copy Code code as follows:

var str = ' 2013-6-7 ';
var re1 =/\d-+/g; Global matching number, horizontal bar, the number of bars is at least 1, matching the result is: 3-6-
var Re1 =/(\d-) +/g; Global match number, horizontal bar, number and bar overall 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

Copy Code code as follows:

var str = ' 2013-6-7 ';
var re =/(\d+) (-)/g;
str = str.replace (re,function ($0,$1,$2) {

If there are children in replace (),
The first parameter: $ (2013-6-), the overall result of the match success.
The second argument: $ (the first group to match the success, which refers to \d 2013, 6)
Third argument: $ (the second grouping that matches success, which refers to---)
return $ + '. '   Return 2013 respectively. 6.

});
alert (str); 2013.6.7
The whole process is to use the subkey to replace the 2013-6--respectively into 2013. 6. Final pop-up 2013.6.7

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

Copy Code code 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)

Add: Exec () method: As with the match method, search for content that conforms to the rules, and returns the content, formatted as an array.

Usage: Regular. exec (string);

Properties: Input (represents the string to match)

Chestnuts: Not a global match:

Copy Code code as follows:

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 match result, and the subkey
alert (r.length); 2 Returns the length of the content
alert (r.input); Now test001 test002 represents a string that matches successfully each time
Alert (r[0]); test001
Alert (r[1]); 001 represents the first subkey in each matching success string (\d+)
alert (R.index); 4 The position of the first character in each successful string

Global match: If it is a global match, you can find the string to match each time through the while loop, along with the subkeys. Every match starts with the last position.

Copy Code code as follows:

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 a match, always match to the end R is false, then stop matching the match to test001 test002
while (R = re.exec (TESTSTR)) {
Alert (R),//returns a string that matches successfully each time, and subkeys, respectively, pop-up: test001 001,test002 002
alert (r.input); Eject separately: Now test001 test002 now test001 test002
Alert (r[0]); The string representing the success of each match pops up: test001 test002
Alert (r[1]); Represents the first subkey (\d+) in each matching success string that pops up: 001 002
alert (R.index); Match the position of the first character in the successful string each time, respectively, by ejecting: 4 12
alert (r.length); Pop-up: 2 2
}

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

Copy Code code as follows:

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
In fact, 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

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.

Copy Code code as follows:

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 matching, the classname must be preceded by a start or a space, followed by the same. 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 of the way to declare the regular, the parameters are string type, so we use the time, we need to ensure that these special characters in the string can also output the line. 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

Copy Code code as follows:

var str = ' Assssjdssskssalsssdkjsssdss ';
var arr = str.split ('); Converts 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 a character, and repeats the character at least once.
Str.replace (Re,function ($0,$1) {
Alert ($); Represents the success of each match: AA DD JJ KK L SSSSSSSSSSSSSSSSS
Alert ($); Represents the first subkey for each match success, i.e. \w:a D J k L S
  
if (index<$0.length) {//If the value saved by index is less than the length of $ $, do the following
index = $0.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 QQ number of the rule 
  1 can not be 0 
  2 must be 5-12 digits of the 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)

Copy Code code as follows:

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 white space followed by a global match
Return Str.replace (Re, '); Replace the space with empty
}

Some of the commonly used form checksums

Copy Code code as follows:

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 so-called warm so know new, can be a teacher, to a lot of contact. Collation of the regular basic knowledge point is about so much, write a bit of chaos, welcome correction.

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.