JavaScript Learning Notes (15)--Date,regexp of objects

Source: Internet
Author: User
Tags gmt time zone local time

In the study of Liaoche predecessors of the JavaScript tutorial, encountered some points needing attention, so as a study notes listed, remind yourself to notice!

If you have the need, welcome to visit the previous blog https://www.liaoxuefeng.com/ study.

Date

In JavaScript, date objects are used to represent dates and times.

System Current Time:

varnow =NewDate (); now;//Thu Nov 20:50:03 gmt+0800 (China Standard Time)Now.getfullyear ();//2017, YearNow.getmonth ();//10, month, note the month range is 0~11,10 represents NovemberNow.getdate ();//30, indicates number 30thNow.getday ();//4, indicating ThursdayNow.gethours ();//20, 24-hour systemNow.getminutes ();//50, MinutesNow.getseconds ();//3, SecondsNow.getmilliseconds ();//167, number of millisecondsnow.gettime ();//1512046203167, timestamp in number form

It is important to note that the current time is the time that the browser obtains from the native operating system and is not necessarily accurate because the user can set the current time to any value.

Note: JavaScript Date Object month value starts from 0, remember 0=1 month, 1=2 month, 2=3 month, ..., 11=12 month.

If you want to create a Date object that specifies the dates and times, you can use:

var New Date (5, +, 123//  Fri June 20:15:30 gmt+0800 (CST)

The second way to create a specified date and time is to parse a string that conforms to the format, which returns a timestamp, and then we can convert the timestamp to a date :

var d = date.parse (' 2015-06-24t19:49:22.875+08:00 '//  1435146562875var New Date (1435146562875//  Wed June 19:49:22 gmt+0800 (CST)//  5

Note: When using date.parse () , the passed-in string uses the actual month 01~12, and getMonth () gets the month value 0~11 after the conversion to the Date object.

Time

The time represented by the date object is always displayed in the browser's time zone, but we can display both the local time and the adjusted UTC time.

var New Date (1435146562875//  ' 2015/6/24 7:49:22 ', local time (in Beijing time zone), the string displayed is related to the format set by the operating system //  ' Wed, June 11:49:22 GMT ', UTC time, 8 hour difference from local time

In JavaScript, we pass a timestamp of number type, and any browser can convert it to the cost of time.

Timestamp: The timestamp is an auto-increment integer that represents the number of milliseconds since the beginning of the GMT time zone at the time of the January 1, 1970 zero. If the time of the browser's computer is accurate, the time stamp numbers are the same in any time zone on the computer in the world. Therefore, timestamps can represent exactly one moment and are independent of the time zone.

We just need to pass the timestamp , or store the timestamp, and let JavaScript automatically convert to local time.

REGEXP (Regular expression)

Strings are the most data structure involved in programming, and the need to manipulate strings is almost ubiquitous.

For example, to determine whether a string is a legitimate email address, although you can programmatically extract @ the substring before and after, and then judge whether it is a word and domain name, but this is not only cumbersome, and code is difficult to reuse.

A regular expression is a powerful weapon used to match strings. Its design idea is to use a descriptive language to define a rule for a string, and any string that conforms to the rule, we think it "matches", otherwise the string is illegal.

We can use regular expressions to determine whether a string is a legitimate e-mail address:

    1. Create a regular expression that matches the email;

    2. Use the regular expression to match the user's input to determine whether it is legal.

Because the regular expression is also represented by a string, we first know how to describe the character with characters.

In regular expressions, if a character is given directly, it is exactly the exact match.

With  \d matches a number ,  \w can match a letter or number,     can match any character   \s  < Span style= "color: #000000;" > match a space (also including tab and other whitespace characters) :

    • &NBSP; " "   can match   ' 007 '  , but cannot match   ' 00A '  
    •   ' \d\d\d '   can match   ' 010 '  
    •   ' \w\w '   can match   ' js '  
    •   ' js. '   can match   ' jsp '  ,   ' JSS '  ,   ' js! '   and so on;

If you need to match a variable-length character, use * to represent any character (including 0), use + to represent at least one character , or 0 or 1 characters with a . {n} represents n characters , and {n,m} represents n~m characters :

One example can be seen:

\d{3}\s+\d{3,8}

Explanation: We look at this regular expression from left to right:

    1. \d{3} indicates a match of 3 digits, e.g. ' 010 ';
    2. \s indicates that a space is matched, so \s+ represents at least one space , such as Match ',' \t\t ' , and so on.
    3. \d{3,8} represents a 3~8 number, such as ' 1234567 ' .

together, the above regular expression can match a telephone number with an area code separated by any space .

What if you want to match a number like ' 010-12345 ' ? because '-' is a special character, in the regular expression, to be escaped with ' \ ' , so, the above is \d{3}\-\d{3,8} .

However, there is still no match for ' 010-12345 ' because there are spaces. So we need more complex ways of matching .

Advanced

To make a more accurate match, you can use [] to represent the range:

    • &NBSP; [0-9a-za-z\_]   can match a number, letter, or underscore;
    • Span style= "color: #000000;" >  [0-9a-za-z\_]+   can match a string consisting of at least one number, letter, or underscore, such as   ' A100 '  ,  ' 0_z '  ,  ' js2015 '   et cetera;
    • [a-za-z\_\$][0-9a-za-z\_\$]*   can match a string consisting of a number, a letter, or an underscore, a letter or an underscore, a $. This is the variable name allowed by JavaScript;
    • &NBSP; [A-za-z\_\$][0-9a-za-z\_\$]{0, 19 The   more precisely restricts the length of the variable to 1-20 characters (1 characters before the top and 19 characters later).

a| b can match A or B, so (j|j) Ava (s|s) cript can match 'JAvaScript ' , 'JAvascript ' , 'JAvascript ' or 'j Avascript ' .

^ represents the beginning of a line , and ^\d indicates that it must begin with a number .

$ represents the end of the line , and \d$ indicates that it must end with a number .

You may have noticed that JS can also match ' JSP ' , but with ^js$ it becomes the whole line match, it can only match ' JS ' .

Regexp

JavaScript has two ways of creating a regular expression:

The first is a direct /Regular expression/ write , and the second is to create a RegExp object from the new RegExp (' regular expression ') :

var re1 =/abc\-001/; var New RegExp ('abc\\-001'//  /abc\-001///  /abc\-001/

Note: If you use the second notation because of the escape problem of the string, the two of the string \\ is actually one \ .

Let's look at how to tell if a regular expression matches:

var re =/^\d{3}\-\d{3,8}$/; re.  Test(//  truere. Test // falseRe. Test // false

The test () method of the RegExp object tests whether a given string conforms to a condition .

Slicing a string

Regular expressions can be used to slice strings more flexibly than with fixed characters:

Normal segmentation code, Unable to recognize contiguous spaces :

' A B   //  [' A ', ' B ', ' ', ' ', ' C ']

With regular expressions, no matter how many spaces can be divided normally :

' A B   //  [' A ', ' B ', ' C ']

to join, try:

' A, B, C  //  [' A ', ' B ', ' C ', ' d ']

Re-join ; try:

' A, b;; C  //  [' A ', ' B ', ' C ', ' d ']

If a user enters a set of tags, they can use regular expressions to convert irregular input into the correct array.

Group

Regular expressions can be used to extract substrings In addition to simple judgments, and () to represent the groupings to be extracted (group).

Regular expression ^ (\d{3})-(\d{3,8}) $ defines two groups that can extract the area code and local numbers directly from the matching string.

var re =/^ (\d{3})-(\d{3,8}) $/; re.  EXEC(//  [' 010-12345 ', ' 010 ', ' 12345 ')re. exec // NULL

If a group is defined in a regular expression, the substring can be RegExp extracted with the exec () method on the object.

The exec () method returns an Array after the match succeeds, the first element is the entire string to which the regular expression matches, and the subsequent string represents the successful substring.

The exec () method returns null if the match fails.

Tips: extracting substrings from regular expressions is powerful.

The following regular expression can directly identify the legal time :

var re =/^ (0[0-9]|1[0-9]|2[0-3]|[ 0-9]) \:(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]| [0-9]) \:(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]| [0-9]) $/,re.exec (//  [' 19:05:30 ', ' n ', ', ', ') '

However, there are times when regular forms cannot be fully validated, such as the date of identification:

var re =/^ (0[1-9]|1[0-2]|[ 0-9])-(0[1-9]|1[0-9]|2[0-9]|3[0-1]|[ 0-9]) $/;

For the ' 2-30 ' , ' 4-31 ' such illegal date, with regular or can not be recognized, or write out to be very difficult, then the need for a program to identify the cooperation.

Greedy match

Greedy mode: Regular expressions tend to match the maximum length, which is called greedy matching.

Non-greedy mode: just match to the result is good, fewer match characters.

The default is greedy mode; Add a question mark directly after the quantifier ? is the non-greedy mode.

For example, match the number following the0:

var re =/^ (\d+) (0*) $/; Re.exec (//  [' 102300 ', ' 102300 ', ']

Because \d+ uses greedy match, directly to the back of 0 all matching, the result 0* can only match the empty string.

You must let \d+ use a non-greedy match (that is, as few matches as possible) in order to match the back 0 , add a ? you can let the \d+ use a non-greedy match:

var re =/^ (\d+?) (0*) $/; Re.exec (//  [' 102300 ', ' 1023 ', ' xx ' )
Global Search

JavaScript regular expressions also have several special flags, the most commonly used is g, which indicates a global match:

var r1 =/test/g; // equivalent to: var New REGEXP (' Test ', ' G ');

Global matching can execute the exec () method multiple times to search for a matching string . When we specify the G flag, each time exec () is run, the regular expression itself updates the LastIndex property to indicate the last index to which it was last matched:

vars = ' JavaScript, VBScript, JScript and ECMAScript ';varre=/[a-za-z]+script/G;//use global match:Re.exec (s);//[' JavaScript ']Re.lastindex;//TenRe.exec (s);//[' VBScript ']Re.lastindex;// -Re.exec (s);//[' JScript ']Re.lastindex;// inRe.exec (s);//[' ECMAScript ']Re.lastindex;// -Re.exec (s);//NULL until the end is still not matched to

Note: The global match is similar to a search and therefore cannot be used, so it /^...$/ will only match at most once.

The regular expression can also specify the I flag, which indicates that the case is ignored, and the m flag indicates that a multiline match is performed .

JavaScript Learning Notes (15)--Date,regexp of objects

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.