"JavaScript advanced Programming" Reading notes-Reference type II: Date and RegExp

Source: Internet
Author: User
Tags character classes instance method iso 8601 month name

3.Date TypeThe date type uses the number of milliseconds from UTC (Coordinated Universal time, international coordination hour) to start at midnight (0 o'clock) January 1, 1970 to save the dates.     With this data storage format, the date type can be saved for exactly 285,616 years before or after January 1, 1970.     Create a Date object: var now=new date (); The current date and time are automatically obtained when the date constructor is called without passing arguments. If you want to create a date based on a specific date and time, you must pass in the number of milliseconds that represent that date (that is, the number of milliseconds from midnight January 1, 1970 to the date).     To simplify this calculation process, ECMAScript provides two methods: Date.parse () and DATE.UTC (). Date.parse () receives a string parameter that represents the date, and then attempts to return the number of milliseconds of the corresponding date based on the string. ECMA-262 no definition date.parse () should support that format, so the behavior of this method varies depending on the implementation and usually varies by region. Browsers that set the region to the United States generally accept the following date formats:
    • "Month/day/year", as in 11/26/2015;
    • "English month name day, year", such as November 26,2015;
    • "English Day of the week English month name: minutes: Seconds time zone", such as Tue 00:00:00 GMT-0700.
    • ISO 8601 extended format yyyy-mm-ddthh:mm:ss.sssz (e.g. 2015-11-25t00:00:00). Only implementations that are compatible with ECMASCRIPT5 support this format.
For example, to create a Date object for May 25, 2015: var somedate=new date (Date.parse ("may 25,2015")); If the string passed into the Date.parse () method cannot represent a date, it returns Nan. In fact, if you pass a string that represents a date directly to the date constructor, the background calls Date.parse ().     For example, var somedate=new Date ("may 25,2015"); The DATE.UTC () method also returns the number of milliseconds that represent the date, but with Date.parse () uses different information when building the value. The parameters of DATE.UTC () are year, based on 0 month (January is 0, February is 1, ...).          ), the Day of the month (1 to 31), the number of hours (0 to 23), minutes, seconds, and milliseconds, only the first two parameters are required, the number of days defaults to 1, and the other is 0.          GMT time January 1, 2000 midnight 0 o'clock Var somedate=new Date (DATE.UTC (2000,0));      GMT May 5, 2005 PM 5:55:55 var somedate=new Date (DATE.UTC (2005,4,5,17,55,55)); ECMAScript5 adds the Date.now () method, simplifying the work of parsing code with a Date object. 4.REGEXP TypeECMAScript supports regular expressions through the regexp type.     Using the following Perl-like syntax, you can create a regular expression: var expression =/pattern/flags; The pattern section can be any simple or complex regular expression that can contain character classes, qualifiers, groupings, forward lookups, and reverse references. Each regular expression can have one or more flags (flags) that indicate the behavior of the regular expression. The matching pattern for regular expressions supports the following 3 flags:
    • G: Represents the global mode, where the pattern is applied to all strings, rather than stopping immediately when the first occurrence is found;
    • I: Indicates case insensitive mode (case-insensitive);
    • M: represents multiline Mode (multiline), which means that when the end of a line of text is reached, it will continue to find out if the next row has an item that matches the pattern.
Therefore, a regular expression is a combination of a pattern with the above 3 flags.      As shown in the following example:/* * matches all instances of "at" in the string */var pattern1 =/at/g;      /* matches the first "bat" or "cat", case-insensitive */var pattern2 =/[bc]at/i;      /* matches all combinations of 3 characters ending with "at", Case-insensitive */var pattern3 =/.at/gi; Similar to regular expressions in other languages, all of the patterns used in the Meta characterMust be escaped. Meta characters in regular expressions include: ( [ { \  ^  $  |  )  ?  *  +  .  ]  }       /*     * matches the first "bat" or "cat", case insensitive      */     var pattern1 =/[bc]at/i;      /*     * matches the first "[Bc]at", Case insensitive      */     var pattern2 =/\[bc\]at/i;       /*      *  match all 3-character combinations ending with "at", case-insensitive      */     var pattern3 =/.at/gi; nbsp;     /*     *  matches all ". At", Case insensitive      */     var pattern4 =/\.at/gi;       The previous example is to define a regular expression in literal form, and the other way to create a regular expression is to use the RegExp constructor. Receives two parameters: one is the string pattern to match, and the other is the optional flag string.      /*     * matches the first "bat" or "cat", case insensitive      */     var pattern1 =/[bc]at/i;      /*     * Same as pattern1      */     var pattern2 = new R Egexp ("[Bc]at", "I");  4.1 regexp Instance PropertiesEach instance of RegExp has the following properties:
    • Global: Boolean value that indicates whether the G flag is set
    • IgnoreCase: Boolean value indicating whether the I flag is set
    • LastIndex: Integer that represents the character position at which to start searching for the next occurrence, starting from 0
    • Multiline: Boolean value that indicates whether the M flag is set
    • Source: The string representation of a regular expression, returned in literal form
4.2 RegExp Instance methodThe main way to regexp objects is to exec (), the method is specifically designed for capturing groups. EXEC () receives a parameter, which is the string to which the pattern is applied, and then returns an array containing the first occurrence information, or null. The returned array, although an instance of array, contains an additional two properties: Index and input. Where index represents the position of the match in the string, and input represents the string to which the regular expression is applied.     In the array, the first item is a string that matches the entire pattern, and the other item is a string that matches the capturing group in the pattern (if there is no capturing group in the pattern, the array contains only one item).     var text= "Mom and Dad and baby"; var pattern =/mom (and dad (and baby)?)?      /gi;     var matches=pattern.exec (text);     alert (Matches.index);     0 alert (matches.input);          "Mom and Dad and baby" alert (Matches[0]);          "Mom and Dad and baby" alert (matches[1]);          "And Dad and Baby" alert (matches[2]);          "and baby" for exec, even if the global flag (g) is set in the pattern, it returns only one match at a time. The second method of a regular expression is Test (), which accepts a string parameter.     var text= "000-00-0000";      var pattern=/\d{3}-\d{2}-\d{4}/; if (pattern.test (text)) {alert ("Match succeeded!      "); }

"JavaScript advanced Programming" Reading notes-Reference type II: Date and RegExp

Related Article

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.