JavaScript Regular Expressions

Source: Internet
Author: User

/*Review Var obj= new number (2), alert (number.min_value), var num = new Number (22E3), alert (num), alert (Isfinite (num));//   Determine if the specified range is exceeded var person={fname: "John", lname: "Doe", Age:25};var txt= ""; for (x in person) {txt=txt + person[x]; } alert (TXT); ------------------------------------------Regular Expressions (regular expression) Creation method: 1. Use the new operator var reg = new RegExp ("box"); alert (reg);//return/box/var reg = new RegExp ("box", "IG");//The optional parameters of the pattern modifier: I-ignore case, G-Global match; M-Multiline match. Alert (reg);//return/box/gi2. create var reg =/box/;alert (reg) using the literal method;//return/box/var reg =/box/ig;alert (reg);//return/ Box/gi---------------------------var pattern = new RegExp ("box"), var str = new String ("box"), Alert (Pattern.test (str))  ;//return Falsevar pattern = new RegExp ("box", "I"); var str = new String ("This is a box"); Alert (Pattern.test (str));//return Truealert (/box/.test ("This is a box!"); /falsealert (/box/i.test ("This is a box!"); /truealert (/box/.exec ("This is a box!"); /return Nullalert (/box/i.exec ("This is a box!"); /return box alert (/bOx/i.exec ("This box is a box!"); /return Boxalert (/box/i.exec ("This box is a box!")); Return Boxalert (/box/ig.exec ("This box is a box!")); /return Boxvar pattern = new RegExp ("box", "I"); var str = new String ("This is a box! That box is a box! "); var result = pattern.exec (str), var result2 = Str.match (pattern), method of//string object alert (typeof result2); alert (result2[0]); var pattern = new RegExp ("box", "IG");//Global match, that is to find all matching items var str = new String ("This box is a box! That box is a box! "); var result = pattern.exec (str);//returns the first matched string alert (result[1]);//return undefined proves that the return is not an array var result2 = Str.match ( pattern);//string Object Method Alert (typeof result2); alert (result2[1]);//return Box proves that the return is an array var pattern = new RegExp ("box", " I "); var str = new String (" This box is a box! That box is a box! "); var result = Str.search (pattern),//string object method Alert (Result),//return 5 returns the first matching position, returns -1var pattern = new RegExp ("box "," I ");//does not turn on global var str = new String (" This box is a box! That box is a box! "); var result = Str.replace (patTern, "Tom"); alert (result);//this Tom is a box! That box is a box!var pattern = new RegExp ("box", "IG");//Turn on global var str = new String ("This box is a box! That box is a box! "); var result = Str.replace (pattern, "Tom"); alert (result);//this Tom is a tom! That's Tom is a tom!var pattern = new RegExp ("!", "IG");//Turn on global var str = new String ("This box is a box! That box is a box! "); var result = Str.split (pattern), alert (result),//this box is a box, which box is a box,var pattern = new RegExp ("!", "IG");// Turn on global var str = new String ("This box is a box! That box is a box! "); var result = Str.split (pattern); for (Var i=0;i<result.length;i++) {alert (result[i]);} var pattern = new RegExp ("!", "IG");//Turn on global var str = new String ("This box is a box! That box is a box! "); var result = Str.split (pattern), for (Rs in result) {alert (RS);//return 0, return 1, return 2}var pattern = new RegExp ("box", "IG");//Turn on global var str = new String ("This box is a box! That box is a box! "); var result = Pattern.test (str);//alert (regexp.input);//this bOx is a box! That box is a Box!alert (regexp.leftcontext), alert (regexp.rightcontext), alert (regexp.lastmatch), var pattern =/g. gle/; . Represents any character other than line break var str = "Google"; alert (Pattern.test (str));//truevar pattern =/go*gle/; o* represents 0 to multiple Ovar str = "Gooogle"; Alert (Pattern.test (str));//truevar pattern =/go+gle/; o+ represents 1 to multiple Ovar str = "Gooogle"; Alert (Pattern.test (str));//truevar pattern =/go?gle/; O? denotes 0, or 1 var str = "Ggle"; Alert (Pattern.test (str));//truevar pattern =/go{2,4}gle/; o{2,4} represents 2 and 4var str = "Google", alert (Pattern.test (str)),//truevar pattern =/go{2}gle/; O{2} indicates that only 2 ovar str = "Google" are present; alert (Pattern.test (str));//truevar pattern =/go{2,}gle/; O{2,} indicates that O appears 2 or more than 2 var str = "Gooogle"; Alert (Pattern.test (str));//truevar pattern =/go[a-z]gle/; [A-z] represents any of the 26 lowercase letters of var str = "Google"; alert (Pattern.test (str));//truevar pattern =/go[0-9]gle/; [0-9] denotes any var str = "Go8gle" in 0~9; alert (Pattern.test (str));//truevar pattern =/go[a-za-z0-9]gle/; [A-za-z0-9] is a compound notation that represents 26Any of the uppercase or lowercase letters or numbers of var str = "Go8gle"; Alert (Pattern.test (str));//truevar pattern =/go[^0-9]gle/; [^0-9] represents any one of the characters in non-0~9 var str = "Go8gle"; Alert (Pattern.test (str));//falsevar pattern =/^go[0-9]gle/; ^ indicates starting from scratch to match var str = "Ggo8gle"; Alert (Pattern.test (str));//falsevar pattern =/go\wgle/; \w denotes any letter, number, or underscore equivalent to [a-za-z0-9_]var str = "Go_gle"; Alert (Pattern.test (str));//truevar pattern =/go\wgle/; \w denotes any non-alphabetic, non-numeric, non-underlined character, equivalent to [^a-za-z0-9_]var str = "Go_gle"; Alert (Pattern.test (str));//falsevar pattern =/go\dgle/ ; \d represents any number, equivalent to [0-9]var str = "Go9gle"; Alert (Pattern.test (str));//truevar pattern =/go\dgle/; \d represents any non-numeric character, equivalent to [^0-9]var str = "Go9gle"; Alert (Pattern.test (str));//falsevar pattern =/google$/; $ indicates starting at end of line to match var str = "Ggoogle"; Alert (Pattern.test (str));//truevar pattern =/goo\sgle/; \s Find white space characters. var str = "Ggoo gle"; Alert (Pattern.test (str));//truevar pattern =/google\b/; \b matches the word boundary. Similar to $var str = "Ggoogle"; Alert (Pattern.test (str));//truevar pattern =/google|baidu|bing/; Vertical Bar | Indicates a match or selectionSelect mode var str = "Ggoogle"; Alert (Pattern.test (str));//truevar pattern =/(google|baidu|bing) s/; Vertical Bar | Indicates matching or selection mode var str = "Ggoogles"; Alert (Pattern.test (str));//truevar pattern =/google{4,8}$/; Indicates the end of E appears 4~8 times var str = "ggoogleeee"; Alert (Pattern.test (str));//truevar pattern =/(Google) {4,8}$/; Brackets denote grouping, indicating that Google appears at the end of 4~8 var str = "Ggooglegooglegooglegoogle"; Alert (Pattern.test (str));//truevar pattern =/8 (. *) 8/; Group var str = "Goo8google8gle";//pattern.test (str);//pattern.exec (str); Str.match (pattern); alert (regexp.$1);// Google regexp.$1 represents the matching string for the first grouping in the get pattern var pattern =/8 (. *) 8/; Group var str = "This is 8google8";d ocument.write (str.replace (Pattern, "<strong>google</strong>")); var Pattern =/8 (. *) 8/; Group var str = "This is 8google8";d ocument.write (str.replace (Pattern, "<strong>$1</strong>")); var pattern = /8 (. *) 8/; Group var str = "This is 8google8";d ocument.write (str.replace (Pattern, "<strong>$1</strong>")); var pattern = /[a-z]/; var str = "You love"; Alert (STR.REplace (Pattern, "1"));//1ou Lovevar pattern =/[a-z]+/; + greedy mode var str = "You love"; Alert (str.replace (Pattern, "1"));//1 Lovevar pattern =/[a-z]+?/; Use +? lazy mode var str = "You love"; Alert (str.replace (Pattern, "1"));//1ou Lovevar pattern =/8 (. *) 8/; Using greedy mode var str = "This is 8google8 8google8 8google8";d ocument.write (str.replace (Pattern, "<strong>$1</ Strong> "));//this is <strong>google8 8google8 8google</strong>var pattern =/8 (. *?) 8/; Using the lazy mode var str = "This is 8google8 8google8 8google8";d ocument.write (str.replace (Pattern, "<strong>$1</ Strong> ")//this is <strong>google</strong> 8google8 8google8 var pattern =/8 (. *?) 8/ G Use lazy mode to turn on global var str = "This is 8google8 8google8 8google8";d ocument.write (str.replace (Pattern, "<strong>$1< /strong> "));//this is <strong>google</strong><strong>google</strong><strong> Google</strong>var pattern =/8 ([^8]*) 8/g; Another kind of inertia that masks 8 matches, turns on global var str = "This is 8GOOGLE8 8google8 8google8 ";d ocument.write (str.replace (Pattern," <strong>$1</strong> "));//this is <strong >google</strong><strong>google</strong><strong>google</strong>var pattern =/8 ([ ^8]*) 8/g; Another inertia, masking 8 matches, turns on global var str = "This is 8google8 8google8 8google8";d ocument.write (str.replace (Pattern, "<strong> $1</strong> "));//this is <strong>google</strong><strong>google</strong><strong >google</strong>var pattern=/(\d+) ([A-z])/;  Capture-type grouping, where all groupings capture returns VAR str= "123abc", var a=pattern.exec (str), alert (a[0]),//123A returns the string matched to alert (a[1]);//123 Returns the string that the first packet matches to alert (a[2]);//a returns the string to which the second packet matches the Var pattern=/(\d+) (?: [A-z])/;  A non-capturing grouping, simply add a: var str= "123abc", var a=pattern.exec (str), alert (a[0]), and//123a return the matched string alert (a[1]);//123 Returns the string that the first packet matches to alert (a[2]);//undefined returns the string Console.log ("Test Zhangweiwen") to which the second grouping matches; var pattern=/(a? B? (c?))) /;//nested matching, from outside to inside var str= "abc"; Alert (Pattern.exec (str));//abc,abc,bc,cvar PattErn=/goo (=gle)/;//must be followed by GLE in order to return to Goovar str= "Google"; alert (Pattern.exec (str));//goovar pattern=/[1-9][0-9]{5}/; var str= "741027";//must be 6 digits, must be a number, first cannot be 0alert (Pattern.test (str)), Alert (Str.match (pattern)), Var pattern=/^\w*\.zip|  7z&/;     Letters, numbers, underscore the name of the compressed file var str= "Study_2015.zip"; Alert (Pattern.test (str)), Var pattern=/(\s+)/g;var str= "Goog le Alert (str.replace (Pattern, ","));//alert ("|"); +pattern.exec (str) + "|"); Alert (Str.match (pattern)), Var pattern=/(\w+)/g;var str= "goog le a"; Alert (str.replace (Pattern, ","));//aler T ("|"  +pattern.exec (str) + "|"); |goog,goog|  The first represents all the groupings returned, and the second GOOG indicates that the first packet returned matches the string alert (Str.match (pattern)); Goog,le,a returns all matches to the string Var pattern=/^\s+ (. +?)   \s+$/; /.+/indicates that the greedy mode/.+?/represents the lazy mode var str= "goog le"; Alert ("|"  +str.replace (Pattern, "$") + "|");  Replace alert (regexp.$1) with the matching result string for the first grouping. You can view the matching results for the first grouping*/

JavaScript Regular Expressions

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.