Super-full JS Regular Expressions organize notes _ regular expressions

Source: Internet
Author: User
Tags character classes character set constructor html tags lowercase

var recat = new RegExp ("Cat", "GI"); The RegExp constructor can take one or two arguments, the first parameter describes the pattern string that needs to be matched, and the second parameter specifies the additional processing command 
var recat =/cat/gi;//Using Perl-style syntax 
 
 I: Performing a case-insensitive match 
 G: Perform a global match (find all matches instead of stopping when the first match is found) 
 m: Perform multiple-line matching 


Metacharacters

Metacharacters is part of the expression syntax, all the metacharacters used in regular expressions are: {[(\ ^ $ |]]}? * + -
such as matching a question mark: var reqmark =/\?/; or var Reqmark = new RegExp ("\"); Note Here are two backslashes, double escape

\xxx find characters specified in octal xxx, such as:/\142/for character B 
\xdd find characters specified in hexadecimal number DD, such as:/\x62/for character B 
\uxxxx find Unicode characters specified in hexadecimal number xxxx, such as:/\u 0062/character B 
\ r Find a carriage return 
\ n Find a newline character 
\f find a page break 
\ Find a tab 
\v find a vertical tab 
\a find an alert character 
\e find an escape character 
\cx finds NULL characters for the control character corresponding to x 
 
. Find a single character, except for newline and line terminator, equivalent to [^\n\r] 
\w lookup Word characters, equivalent to [a-za-z_0-9] 
\w find non-word characters , equivalent to [^a-za-z_0-9] 
\d lookup number, equivalent to [0-9] 
\d Looking for non-numeric characters, equivalent to [^0-9] 
\s looking for whitespace characters, equivalent to [\t\n\x0b\f\r],\x0b for vertical tab and \ T 
\s look for non-white-space characters, equivalent to [^ \t\n\x0b\f\r] 

Brackets

[ABC] find any character between square brackets 
[^ABC] Find any characters that are not in brackets 
[0-9] find any number from 0 to 9 [a-Z 
] find any character from small to lowercase z [a-Z 
] find any uppercase Letters A to uppercase Z [a- 
z] find any character from uppercase A to lowercase z 
[adgk] find any character within a given set 
[^ADGK] find any character outside a given collection 

quantifiers
? Matches any string containing 0 or one, such as: Ba?d match BD, bad
+ Match any string containing at least one, such as: Ba+d match bad, Baad
* Match any string containing 0 or more, such as: Ba*d match BD, Bad, Baad
{n} matches a string containing a sequence that happens to occur n times, such as: Ba{1}d match bad
{N,m} matches a string containing at least n times but not more than m times, such as: ba{0,1}d matching BD, bad
{N,} matches a string containing at least n occurrences of a sequence, such as: ba{0,} matching BD, bad, Baad, Baaad
greedy quantifiers: First Look at the entire string is matched, if found no match, last year the last character in the string and try again, such as:?, +, *, {n}, {n, m}, {n}, default is greedy quantifier
Lazy quantifiers: First look at the string to match the first letter, if this single character is not enough, the next character, a two-character string, is the opposite of how greedy quantifiers work, such as:??、 +?, *?, {n}?, {n, m}?, {n,}?
Dominant quantifier: Only try to match the entire string, if the entire string does not produce a match, do not make further attempts, such as:? +, + +, *+, {n}+, {n, m}+, {n,}+

var stomatch = "abbbaabbbaaabbb1234"; 
var re1 =/.*bbb/g; The match result is "abbbaabbbaaabbb" 
var re2 =/.*?bbb/g;//Only the inert classifier can match successfully, the result is "abbb", "aabbb", "aaabbb" 
var re3 =/.*+bbb/g ; No match, direct error 

Grouping of complex patterns: using a series of parentheses to surround a series of characters, character classes, and quantifiers.
/(dog) {2}/match "Dogdog"
/([Bd]ad?) * * Match empty, "Ba", "Da", "bad", "Dad"
/(MOM (and dad)?) /Match "Mom", "Mom and Dad"
/^\s* (. *?) \s+$/matches the white space character of both end and end, also can use/^\s+|\s+$/g
reverse references for complex patterns: also called capture groupings, created and numbered in the order of left parenthesis characters encountered from left to right, such as expressions (A? B? (C))) The three reverse references that will result in numbering from 1-3 are: (A? B? (C))), (B? C.)), (c.)
There are several different ways to use reverse references:
First, after you use the test (), match (), or search () method of the regular Expression object, the value of the reverse reference can be obtained from the RegExp constructor, such as:

var stomatch = "#123456789"; 
var renumbers =/# (\d+)/; 
Renumbers.test (Stomatch); 
alert (regexp.$1); "123456789", save the first reverse reference, in turn, you can use $2,$3 ... 

You can then include a reverse reference directly in the expression that defines the grouping, using special escape sequences such as \1, \2, and so on to implement

var stomatch = "Dogdog"; 
var Redogdog =/(dog) \1/; Equivalent to/dogdog/ 
alert (redogdog.test (Stomatch));//true 

Third, a reverse reference can be used in the replace () method of a String object, by using special character sequences such as $, $, and so on to implement

var stochange = "1234 5678"; 
var rematch =/(\d{4}) (\d{4})/; 
Alert (Stochange.replace (rematch, "$ $")); "5678 1234" 

candidate for a complex pattern: using a pipe character (|) Between two separate patterns.

var rebadwords =/badword | Anotherbadword/gi; 
var suserinput = "This is a String using Badword1 and Badword2."; 
var sfinaltext = suserinput.replace (rebadwords, function (smatch) {return 
 smatch.replace (/./g, "*"); Replace each letter in the sensitive word with an asterisk 
); 

non-capturing grouping of complex patterns: No reverse reference is created compared to capture groupings, in longer regular expressions, storing a reverse reference lowers the matching speed and can still have the same capabilities as a matching string sequence by using a non-capturing grouping, without the overhead of storing the results

var stomatch = "#123456789"; 
var renumbers =/# (?: \ d+)/; You can create a non-capture grouping 
renumbers.test (stomatch) simply by adding a question mark and a colon followed by the left parenthesis; 
alert (regexp.$1); "", the output empty string is because the group is 
stomatch.replace alert (renumbers, "abcd$1"));//The output is "abcd$1" instead of "abcd123456789", Cannot use any reverse references 

Another example:

String.prototype.stripHTML = function () { 
 var retag =/< (?:. | \s) *?>/g; Match all HTML tags to prevent the insertion of malicious HTML code return 
 this.replace (Retag, ""); 
} 

The prospect of complex patterns: telling the regular expression operators to look forward with some characters without moving their position, there is a forward looking (check for a particular character set that follows) and a negative outlook (check for the next specific character set that should not appear)
Forward-looking (? =n) matches any string followed by the specified string n but excluding N, note that the parentheses here are not grouped
Negative forward (?!) n matches any string that is not followed by the specified string n, such as:

var sToMatch1 = "bedroom"; 
var sToMatch2 = "Bedding"; 
var reBed1 =/(Bed (? =room))/; 
var reBed2 =/(Bed (?!) room))/; 
Alert (Rebed1.test (STOMATCH1)); True 
alert (regext.$1);//output "bed" instead of "bedroom" 
alert (Rebed1.test (STOMATCH2));//false 
alert ( Rebed2.test (STOMATCH1)); False 
alert (Rebed2.test (STOMATCH2));//true 
alert (regext.$1)//output is also "bed" 

boundary of complex pattern: The position used to represent patterns in regular expressions
n$ matches any string ending with N, such as:/(\w+) \.$/matches the End-of-line word "one.", "two." Wait
^n matches any string that starts with N, such as:/^ (. +?) \b/one or more word characters after the start position
\b Find matches at the beginning or end of a word, such as:/\b (\s+?) \b/g or/(\w+)/g match extract word from string
\b Find a match that is not at the beginning or end of a word
Multiple-line mode of complex mode:

var stomatch = "The Second\nthird fourth\nfifth sixth"; 
var relastwordonline =/(\w+) $/gm; 
Alert (Stomatch.match (relastwordonline)); Output ["Second", "fourth", "sixth"] and not just "sixth" 

properties and methods of RegExp objects:
Whether the global//regexp object has a flag g
IgnoreCase//regexp object has flag I
Multiline//regexp object has a flag m
Source text//Regular expression
lastindex//An integer that indicates which character position the next match'll will start from (only if the EXEC () and test () functions are filled in, otherwise 0)
The real use is lastindex, such as:

 
var stomatch = "BBQ is short for barbecue"; 
var ReB =/b/g; 
Reb.exec (Stomatch); 
alert (Reb.lastindex); 1, the matching position is 0,lastindex 1 
reb.exec (stomatch); 
alert (Reb.lastindex); 2 
reb.exec (stomatch); 
alert (Reb.lastindex); 
reb.lastindex = 0;//start to match 
reb.exec (stomatch); 
alert (Reb.lastindex); 1, not 21. 

Static Properties
Input, short name $_, last used for matching string (passed to exec () or test ())
Leftcontext, short named $^, the substring preceding the last match
Rightcontext, short named $^, substring after last match
Lastmatch, short named $&, last matched character
Lastparen, short named $+, last matched grouping
Multiline, short named $*, is used to specify whether all expressions use a multiline pattern of Boolean values, unlike other properties, that do not rely on the last performed match, it can set the M option for all regular expressions, Regexp.multiline = "true"; Note that IE and opera do not run it

 var Stomatch = "This has been a short, short summer"; 
 var reshort =/(s) hort/g; 
 Reshort.test (Stomatch); 
 alert (regexg.input); "This is has been a short summer"; 
 alert (Regexg.leftcontext); "This has been a"; 
 alert (Regexg.rightcontext); ", Short summer"; 
 alert (Regexg.lastmatch); "Short" 
 alert (Regexg.lastparen);//"s" 
 
compile ()//compiling regular expression 
alert (Recat.exec ("A cat, a cat, a cat cat") ; Returns an array in which the first entry is the first match and the other is the reverse reference 
alert (Recat.test ("cat"));//true, retrieves the value specified in the string, returns True or false. 

Methods that support string objects of regular expressions

var stomatch = "A bat, a cat, a fat, a fat cat"; 
var reat =/at/gi; 
Alert (Stomatch.match (reat)); Returns the array alert (Stomatch.search (reat)) that contains all the matches in the string, or the 
first time the output appears in the string at position 3, the global match G does not work on search () 
alert ( Stomatch.replace (Reat, "Dog")); Replaces the substring 
alert (stomatch.replace (Reat, function (smatch) {return "Dog") that matches the regular expression 
; 
Alert (Stomatch.split (/\,/)); Dividing a string into an array of strings 

Common mode  
Date:/(?: 0 [1-9]| [12] [0-9]|3[01]) \/(?: 0 [1-9]|1[0-2]) \/(?: 19|20\d{2})/ 
url:/^http://([w-]+.) +[w-]+ (/[w-./?%&=]*)?$/ 
E-mail address:/^ (?: \ W+\.?) *\w+@ (?: \ W+\.?) *\w+$/ 
Domestic phone number:d{3}-d{8}|d{4}-d{7} 
Tencent QQ number:[1-9][0-9]{4,} 
Zip code: [1-9]d{5} (?! D  
ID:d{15}|d{18} 
IP address:d+.d+.d+.d+ 
Chinese character: [u4e00-u9fa5] 
Double-byte characters (including Chinese characters): [^ x00-xff] 
    string.prototype.len=function () {return This.replace ([^x00-xff]/g, "AA"). Length;}  
Full-width character:/[^uff00-uffff]/g 
match a specific number: 

^[1-9]\d*$//Matching positive integer 
^-[1-9]\d*$//matching negative integer 
^-?[ 1-9]\d*$//matching integer 
^[1-9]\d*|0$//matching nonnegative integer (positive integer + 0) 
^-[1-9]\d*|0$//matching non positive integer (negative integer + 0) 
^[1-9]\d*\.\d*|0\.\d*[1-9] \d*$//matching positive floating-point number 
^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*) $///matching negative floating-point number 
^-? ( [1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0) $///matching floating-point 
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$//matching nonnegative floating-point number (positive floating-point number + 0) 

is not very comprehensive, very detailed, feel good about this article good collection, JS regular expression is very important to learn, we must study well.

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.