Examples of JavaScript Regular Expressions and examples of Regular Expressions

Source: Internet
Author: User

Examples of JavaScript Regular Expressions and examples of Regular Expressions

I don't need to talk much about it. I will simply dedicate the code summarized and summarized over the years to you.

<Script type = "text/javascript"> var str2 = "YouCan3You8Up, no can no bibi! "; Var reg =/^ [0-9a-zA-Z] {1, }$/; console. log (reg. test (str2); // General Regular Expression format // create a regular expression object: // var object name = new RegExp (parameter 1, parameter 2 ); // parameter 1: matching rule, which must be a string // parameter 2: can be omitted, (attribute) optional value, used to set the range of matching rules // The first method // determine whether the string contains a substring var str3 = "abcd"; var reg2 = new RegExp ("AB "); console. log (reg2.test (str3); // success: true: false // to match a string, you must use the regular expression function // test (string: used to determine whether the string meets the regular expression rules. If yes, true is returned. If not, returns false // The most common method for creating a regular expression-method 2 // var Object Name =/Parameter 1/parameter 2; // parameter 1: Set the matching rule // parameter 2: attribute (optional), set the range of matching rules var str4 = "youbd "; // determine whether/mn/contains the self-string mnvar reg3 =/ou/; console. log (reg3.test (str4); // parameter 2: attribute (with three values) // I: matching is not case sensitive; // g: Global Search: even if the corresponding content is matched, the matching will continue until the end position of the string // m: multi-line search var reg4 =/jingdong/I; console. log (reg4.test ("JingDongShangCheng"); // match the rule // 1. for normal character matching, // 12 // AB // ac8 /... // 2. special Character // \ n line feed // \ t switch symbol tab key // \ r enter key // \ d number 0-9 or [0123456789] // \ D non-Number or [^ 012 3456789] // \ w letters, numbers, underscores, Chinese characters // \ W blank (space, line feed, tab switch key) // \ S non-blank //. other characters except \ n or [^ \ n] // [] are used to match the character in []. If yes, it is true and does not appear, false // [^] is used to match the character var reg5 =/\ n/except []; var str5 = "LaMo \ nSi"; console. log (reg5.test (str5); var reg6 =/\ d/; // console starting with a number. log (reg6.test ("007t7"); var reg7 = // \ D/; console. log (reg7.test ("3403l"); var reg8 = // \ w/; console. log (reg8.test ("$-@ 9"); var reg9 =/\ W /; Console. log (reg9.test ("$-@ 9"); var reg10 = // \ s/; console. log (reg10.test ("longgui lamosi"); var reg11 = // \ S/; console. log (reg11.test ("longguilamosi"); var reg12 =/[AB]/; // determines whether any character (a, B, AB) exists in the string, true. Otherwise, falseconsole. log (reg12.test ("dafbcd"); // The regular expression corresponding to a special character // 1. select a character. For example, [0-9] matches numbers ranging from 0 to 9. // [a-z] matches all lowercase letters; // [A-Z] That matches all uppercase letters; var reg13 =/[a-z0-9A-Z]/g; console. log (reg13.test ("aii 9B "); // match (Regular Expression) function // function: Used to match whether the specified string matches the rule. The returned value is an array, each string acts as an array element var str13 = "A * 99abcY0B49L"; var arr = str13.match (reg13); console. log (arr); var str14 = "* abc99! @ A3ab "; var reg14 =/AB/g; var arr2 = str14.match (reg14); console. log (arr2); // search (Regular Expression) function // function: returns the start subscript of the string if it matches a string that complies with the rules. Otherwise, return-1var str15 = "youabcdY90we"; var result = str15.search (/AB/); console. log (result); var str16 = "you can you up, no can no bibi! "; Console. log (str16.split (""); console. log (str16.split ("o"); // regular expression match as the separator console. log (str16.split (/[ao]/); var str17 = "1362388064@qq.com"; console. log (str17.split (/\. /));//\. escape Character // use. or @ is used as the separator console. log (str17.split (/[@.] /); console. log (str17.split (/[@ \.] /); // replace (Regular Expression, string) var str18 = "no zuo no dai"; console. log (str18.replace (/o/g, "L"); console. log (str18.replace (/[no]/g, "B"); // Regular Expression Quantifiers in the formula // 1. {n} repeated at least n times // 2. {m, n} repeated m to n times // 3. + matching is equivalent to {1,} 1 to infinity // 4. * Equivalent to {0,} 0 to infinity // 5 .? Equivalent to {0, 1} 0-1; // var reg19 = "aasdfghjklzxcvb"; // var reg19 =/a {2}/g; // var reg19 =/a {1 ,} /g; // var reg19 =/a {3, 6}/; // var reg19 =/a +/; // var reg19 =/*/; var reg19 =/? /G; console. log (reg19.test (str19); var str20 = "youaabcad"; console. log (str1_replace (/? /, "A"); console. log (str1_replace (/? /G, "A"); // determine the zip code (whether the string meets the zip code) // six digits, and all digits are var email = "450000 "; // Form 1 // var regEmail =/^ \ d {6} $/g; // second form var regEmail =/^ [0-9] \ d {5} $/g; console. log (regEmail. test (email); // 2. location operator example: 1. ^ starts with xx. $ ends with xx. // 2. \ B: match the boundary var str21 = "abcde"; // var reg21 =/^ abc /; // start with abc // var reg21 =/^ [abc]/; // var reg21 =/[^ abc]/starting with a, B, or c; // character console except a, B, or c. log (reg21.test (str21); var str22 = "mouse money eye see "; // \ B + word (mo matching the boundary before the word) console. log (str22.match (/\ bmo/g); // word + \ B (ey that matches the boundary after the word) console. log (str22.match (/ey \ B/g); // exercise: Determine whether a string matches the phone number var phoneNum = "13849007907 "; var regPhone =/^ 1 [34578] \ d {9} $/; console. log (regPhone. test (phoneNum); // determines whether the Chinese character var word = "longgui "; var regWord =/[\ u4e00-\ u9fa5] +/g; console. log (regWord. test (word); // identify the 18-digit var num = "410184199504300000" of the ID card number; var regNum =/^ [1-9] \ D {16} [\ d | x] $/; // "^ \ d + $" // non-negative integer (positive integer + 0) // "^ [0-9] * [1-9] [0-9] * $" // positive integer // "^ (-\ d +) | (0 +) $ "// non-positive integer (negative integer + 0) // "^-[0-9] * [1-9] [0-9] * $" // negative integer // "^ -? \ D + $ "// integer //" ^ \ d + (\. \ d + )? $ "// Non-negative floating point number (Positive floating point number + 0) //" ^ ([0-9] + \. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $ "// Positive floating point/" ^ (-\ d + (\. \ d + )?) | (0 + (\. 0 + )?)) $ "// Non-Positive floating point number (negative floating point number + 0) //" ^ (-([0-9] + \. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $ "// negative floating point // quantity //" ^ (-? \ D +) (\. \ d + )? $ "// Floating point number //" ^ [A-Za-z] + $ "// string consisting of 26 English letters //" ^ [A-Z] + $ "/ /a string consisting of 26 uppercase letters // "^ [a-z] + $" // a string consisting of 26 lowercase letters // "^ [a-Za-z0-9] + $ "// a string consisting of digits and 26 English letters //" ^ \ w + $ "// a string consisting of digits, 26 English letters, or underscores // "^ [\ w-] + (\. [\ w-] +) * @ [\ w-] + (\. [\ w-] +) + $ "// email address //" ^ [a-zA-z] +: // (\ w + (-\ w +) *)(\. (\ w + (-\ w + )*))*(\? \ S *)? $ "// Url // ^ 13 \ d {9} $/gi mobile phone number Regular Expression/* // determine if it is positive/^ \ d {1 ,} $ // determine the QQ number between 5 and 11 digits of the QQ number/^ [1-9] \ d {4, 10} $ // determine whether it is an integer (positive or negative integer) /^ \-{0, 1} \ d {1,} $ // or/^ \-? \ D + $ // decimal point/^ \-? \ D + \. \ d + $/* // group symbol // (); // nba {2}/g ---- nbaa sub-string // (nba) {2}/g ----- nbanba sub-string // n (ba) {2}/g ----- nbaba sub-string // [nba] {2}/g ----- any two characters (aa bb nn // nb na ba) </script>

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.