JavaScript Regular Expressions

Source: Internet
Author: User

/** * Created by Administrator on 2015/4/9. */var Result;var log = function (result) {    console.log (result);};/ /1 matches end of number//30cac0040 = 0040//3sfadf92 = 92///\d+$/g:1 \d match number; 2 + one or more occurrences; 3 $ end Occurrences 4 G global search var str1 = ' 30 CAC0040 ',    str2 = ' 3sfadf92 '; result = Str1.match (/\d+$/g);//0040result = Str2.match (/\d+$/g);//92//2 regular match whitespace problem//  The character interval within the string has an indefinite number of spaces, requiring a uniform space//  /\s+/g 1 \s match space; 2 + one or more occurrences; 3 G global Search str1 = '  blue   think '; var reg =/\s+/g;str1 = Str1.replace (Reg, ");//Blue Ideal log (STR1);//Determine if the string is composed of numbers//  /^\d+$/;  1 ^ start; 2 $ end 3 + occurrences 0 or more times str1 = ' Asdw2562fsz '; str2 = ' 123456 '; reg =/^\d+$/;log (Reg.test ('));//falselog (Reg.test (str2)); True

  

4 cell phone number Regular expression//4.1 ignore the preceding 0, support 13, 15 start///^0? (13|15) \d{9}$/1 ^0? : 0 appears 0 times or once; 2 (13|15) 13 or 3 \d{9}$ number 9 times and ends str1 = 013999999999;reg =/^0? (13|15) \d{9}$/;log (Reg.test (str1));

  

5 Remove the space/  //^\s+|\s+$/g 1 ^\s+  One or more spaces out of the starting position; 2 \s+$ ends with one or more spaces 3 | or//5.1 remove the left and right spaces var trimall = function (str) {    return str.replace (/^\s+|\s+$/g, ');};/ /5.2 Remove left space var Lefttrim = function (str) {    return str.replace (/^\s+/g, ');};/ /5.3 Remove the space on the right var Righttrim = function (str) {    return str.replace (/\s+$/g, ');}; str1 = '         asdasf        '; log (' Trim: '); log (Trimall (str1)); log (' Left Trim: ') log (Lefttrim (str1)); log (' Trim: ') Log (Righttrim (str1));

  

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.