The match (), replace () mate of a String object in JavaScript uses the regular expression

Source: Internet
Author: User
Tags blizzard

Regular expressions are long-standing, and the Find-and-replace feature is very powerful, but templates are difficult to remember. The 2 methods of the match (), replace () of the string object in JavaScript use a regular expression template. When the template content does not match the string, match () returns Null,replace () to return the original string. Template object for regular expressions//standard notation regexp = new RegExp (pattern[, flag]); Pattern:The use of templates is key and the main content of this chapter. Flag: Combination of "I" (ignore), "G" (Global), "M" (multiline) I-ignoring case, G-re-retrieval, M-multiple rows Retrieve flag without G, return string, return string array with GvarReg =NewREGEXP ("Blizzard", "G");varstr = "Wind and rain send spring to the snow, snow and sky to heaven Horizontal";varxx = str. (Str.match (reg));//Results of XX: xx[0]= "Snow" alert (xx);//xx[1]= "Flying Snow"//Common wording regexp = new RegExp (/pattern/[flag]);//Remove the quotation mark comma instead of "/"AR reg =NewREGEXP (/Snow/g);varstr = "Wind and rain send spring to the snow, snow and sky to heaven Horizontal";varxx = str. (Str.match (reg));//Results of XX: xx[0]= "Flying Snow"Alert (XX);//xx[1]= "Flying Snow"//Omit notationString.match (/pattern/flag); The entire template object is omitted, allowing JavaScript to automatically generate//var reg = new RegExp (/Blizzard/g);varstr = "Wind and rain send spring to the snow, snow and sky to heaven Horizontal";varxx = str. (Str.match (/snow/g));//Results of XX: xx[0]= "Flying Snow"Alert (XX);//xx[1]= "Flying Snow"the syntax of a template (that is, formal performance)//string matches (including single character) "String"Reg =NewREGEXP (/abc/ig);//Match string "abc", multiple, case-insensitiveReg =NewREGEXP (/abc/IG); XX= "123ABCxyzbcaLMNAbC". Match (REG);//xx is: abc,abc note; BCA does not meet//matches any one of several strings in the "|"Reg =NewREGEXP (/abc|xyz/IG);//between the string ABC or XYZ use | split Note: "|" does not apply to the following [...] ContentReg =NewREGEXP (/abc|xyz/ig); xx= "123ABCxyzbcaLMNAbC". Match (REG);//xx is: ABC,XYZ,ABC//match any one of the characters in the string "[...],[.-.] "REG1 =NewREGEXP (/[abc]/ig);//match any one character with [...]REG2 =NewREGEXP (/[m-p]/ig);//consecutive strings are available with a minus sign [.-.]REG3 =NewREGEXP (/[0-9]/g);//determine if a number is includedREG4 =NewREGEXP (/[a-z]/ig);//determine if the English alphabet is included//Note: "-" is only used for [...] InREG1 =NewREGEXP (/[abc]/ig); xx= "123ABCopqbcaLMNAbC". Match (REG1);//xx is: A,b,c,b,c,a,a,b,cREG2 =NewREGEXP (/[m-p]/ig); yy= "123ABCopqbcaLMNAbC". Match (REG2);//yy is: O,p,m,n//any character in the string does not contain a match "[^ ...] "REG1 =NewREGEXP (/[^abc]/ig);//match any one of the characters is not included with ^REG2 =NewREGEXP (/[^m-p]/ig);//any character that does not contain MNOPREG3 =NewREGEXP (/[^0-9]/g);//determine if no numbers are includedREG4 =NewREGEXP (/[^a-z]/ig);//determine if the English alphabet is not includedNote: "^" is in the [...] Inside, not what is said in the back "^"in [...] Outside REG1=NewREGEXP (/[^abc]/ig); xx= "123ABCopqbcaLMN". Match (REG1);//xx is: 1,2,3,o,p,q,l,m,nREG3 =NewREGEXP (/[^0-9]/g); yy= "123opqLMN". Match (REG3);//yy is: O,p,q,l,m,n//multiple Duplicate characters match "{m,n}"description: {m,n} indicates that a repeating character from M to n matches, and M,n is an integer greater than or equal to 0. {m} and {m,} are correct, {, n} syntax is good, but never matches REG1=NewREGEXP (/abc{2}/ig);//equivalent to "/abcc/ig"REG2 =NewREGEXP (/abc{1,2}/ig);//equivalent to "/abc|abcc/ig"REG3 =NewREGEXP (/abc{0,2}/ig);//equivalent to "/ab|abc|abcc/ig"REG4 =NewREGEXP (/abc{0,}/ig);//Match AB back with any CReg5 =NewREGEXP (/abc{1,}/ig);//Match AB followed by more than one CNote: "{m,n}" does not apply to [...] Zhong Reg1=NewREGEXP (/abc{2}/ig); xx= "ABCABCCABCCC". Match (REG1);//xx is: ABCC,ABCCREG3 =NewREGEXP (/abc{0,2}/ig); yy= "ABXABCCABCCC". Match (REG3);//yy is: ab,abc,abccZZ = "AbA ba ba B". Match (/a {0,}b/ig);//matches any space between AB. ZZ: Ab,a b,a b,a BWW = "aa1bb22cc321dd9876". Match (/[0-9]{2}/g);//matches a 2-digit number. WW is: 22,32,98,76//a subexpression is a part of a string that matches "(...) "Description: Manipulating a part of a string can be enclosed in () REG1 =NewREGEXP (/abc{2}/ig);//equivalent to "/abcc/ig", C was repeated 2 timesREG2 =NewREGEXP (/A (BC) {2}/ig);//equivalent to "/abcbc/ig", BC was repeated 2 timesREG1 =NewREGEXP (/abc{2}/ig); xx = "ABCABCBCCC". Match (REG1);//xx is: nullREG2 =NewREGEXP (/A (BC) {2}/ig); yy = "ABCABCBCCC". Match (REG2);//yy is: ABCBCwildcard characters//matches a single arbitrary character (not including/N) "."xx = "At act Ant Amount". Match (/A.T/GI);//There are 1 characters between A and T. Xx=act,antxx = "At act Ant Amount". Match (/A....T/GI);//There are 4 characters between A and T. Xx=amount//any matching character or string "*" is equivalent to {0,}The asterisk "*" must be preceded by at least one character, and can be used when any match is made. Instead of the character "*"Asterisk cannot precede with qualifier {...} and its abbreviated form XX= "At act Ant Amount". Match (/A*T/GI);//T front any A. Xx=at,t,t,txx = "At act Ant Amount". Match (/A.*T/GI);//any character between A and T. Xx=at,act,ant,amount xx = "At act Ant Amount". Match (GI); Syntax error (* cannot precede qualifier)xx = "At act Ant Amount". Match (/.*OU/GI);//any character in front of the OU. Xx=at Act Ant Amouxx = "At act Ant Amount". Match (/.*/GI);//same as the original string. Xx=at Act ant Amount//any match of subexpression "?" is equivalent to {0,1}“?” There must be at least one character in front of the question mark, which can be used when any match. Instead of the character "?" There is no use in front of the question mark (...) Surround subexpression, "?" Equivalent to "*" rather than "." “?"The main function of a question mark is to (...) {.,.} Operation XX= "at Act,ant amount". Match (/A?T/GI);//there are 0 or 1 A in front of T. Xx=at,t,t,txx = "at Act,ant amount". Match (/?T/GI);//syntax error (* Front no characters)xx = "at Act,ant amount". Match (/A{1}?/GI);//matches the result of "a{1}". Xx=a,a,a,a//more than 1 times the subexpression matches "+" equivalent to {1,}The "+" plus sign must have at least one character in front of it, or you can use "." Instead of the character "+" the plus sign is not used before (...) Surround the subexpression, "+" equals "? "or" *"instead of". " XX= "at Act,ant amount". Match (/A+T/GI);//there are more than 1 a in front of T. Xx=atxx = "at Act,ant amount". Match (/+T/GI);//syntax error (* Front no characters)xx = "at Act,ant amount". Match (/A{1}+/GI);//matches the result of "a{1}". Xx=a,a,a,a//Summary of pass flushed characters“.” The matched character is arbitrary and has no relation to the character in front of the symbol "?" The matching character or string is related to its preceding character or subexpression. Match a character with "*", "+" and "? "After combining, can match 0 or more"? " Not in front of (...) Sub-expression, it matches the previous character "?" There are (...) in front of you. Sub-expression, matches the previous subexpression "?"{m,n} qualifier preceded, the number of matches is {m,n} specified number of XX= "at Act,ant amount". Match (/A.T/GI);//xx=act,antxx = "at Act,ant amount". Match (/A?T/GI);//xx=at,,a,,t,,a,,t,,a,,,,, T,xx = "at Act,ant amount". Match (/A*T/GI);//xx=at,t,t,txx = "Atat fatt,gatat amount". Match (/AT?/GI);//X=at,at,at,at,at,axx = "Atat fatt,gatat amount". Match (/(at)?/gi);//xx=at,at,,, at,,,, at,at,,,,,,,,xx = "Atat fatt,gatat amount". Match (/(at) {2}?/gi);//Xx=atat,atatLocator Start "^ "and end" $ "position if there is/N or/R, use XX in conjunction with multiline= "I am a student". Match (/^s/i);//Xx=nullxx = "I am a student". Match (/^i/i);//xx=ixx = "I am a/nstudent". Match (/^s/i);//Xx=nullxx = "I am a/nstudent". Match (/^s/mi);//Xx=sxx = "I am a student". Match (/m$/i);//Xx=nullxx = "I am a student". Match (/m$/i);//xx=mxx = "I am a/n student". Match (/a$/i);//Xx=nullxx = "I am a/n student". Match (/a$/mi);//Xx=axx = "I am a student". Match (/^s/i);//Xx=nullxx = "I am a student". Match (/^i/i);//xx=ixx = "I am a/nstudent". Match (/^s/i);//Xx=nullxx = "I am a student". Match (/^i.*t$/i);//xx=i am a studentEscape Character/b matches a word boundary (example: carriage return, newline, space, comma). /b/b except xx = "I am a student". Match (/stu/b/i); Xx=null xx = "I am a student". Match (//bstu/i); Xx=stuxx = "I am a student". Match (/ent/b/i); xx=ent xx = "I am a student". Match (/ent/b/i); Xx=null xx = "I am a student". Match (/student/b/i); Xx=null xx = "I am (student) student". Match (/ent/b/i); xx=ent xx = "I am a student student". Match (/ent/b/i); Xx=ent/The CA matches a "Ctrl + a" character./D matches a "number". equivalent to [0-9]./D/D except/F matches a "page break" character. Equivalent to/x0c and/CL./n matches a "newline character". Equivalent to/x0a and/CJ./R matches a "carriage return character". Equivalent to/x0a and/CJ./s matches a "white space character." Includes spaces, tabs, page breaks, and so on. equivalent to [/f/n/r/t/v]. /S/D except/t matches a "tab". Equivalent to/x09 and/ci./V matches a "vertical tab". Equivalent to/x0b and/ck./W matches an "English number". equivalent to [a-za-z0-9]./w/w except/onnn matches a "8 binary number"./XNNN matches a "16 binary number".(slightly)/other characters (symbols used in regular expressions and single double quotes) "/(" "/)" "/[" "/]" "/{" "/}" "/" ""/""/^" "/$" " //" " //"

The match (), replace () mate of a String object in JavaScript uses the regular expression

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.