JavaScript Learning note "Four"

Source: Internet
Author: User

reference types, date types, and regular expressions in JavaScript regexp

One, date type,

The 1.Date constructor,

var now = new Date (); The default now is the current time of the system,

var now = new Date (2007, 5, 3, 12, 21, 40); Create the now, constructor parameters for the specified date, respectively, year, month, day, time, minute, second, millisecond,

June 3, 2007 is 12:21 40 seconds

var now = Date.now (); The current time is the number of milliseconds from January 1, 1970 0 o'clock (UTC time),

2.Date method of Application,

var now = new Date ();

var year = Now.getfullyear (); Gets the four-digit year of the current date [2014]

var month = Now.getmonth (); The month that gets the current date, the current month is November, and the return value is 10 (the month defaults from 0),

var date = Now.getdate (); Gets the date in the current month,

var day = Now.getday (); Get the current time is the day of the week,

var hour = now.gethours (); Gets the current date of the hour,

var minute = Now.getminutes (); Gets the minute of the current date,

var second = Now.getseconds (); Gets the number of seconds of the current date,

var millisecond = Now.getmilliseconds (); Gets the number of milliseconds for the current date,

Each of these methods has a corresponding set method and Setutc method,

Second, the regexp regular expression,

1. Literal and constructor functions,

var pattern =/[ab]c/; Constructs a regular expression by literal means,

var pattern = new RegExp (' [ab]c '); Constructs a regular expression through a constructor, when the constructor is used, the constructor's arguments must be a string,

The pattern of regular expressions,

(1) [g] means that the global pattern, that is, the pattern will be applied to all strings, not immediately stop when the first occurrence is found,/[ab]c/g

(2) [i] indicates case-insensitive mode, when determining matches, ignoring pattern and string case,/[ab]c/i

(3) [M] represents a multiline pattern, that is, when the end of a line of text is reached, the next row is also found for the existence of a pattern-matching item,/[ab]c/m

The properties of the regular expression

A.>global whether the G flag is set

B.>ignorecase whether I flag is set

C.>lastindex starts searching for the starting position of the next string, starting from 0,

D.>multiline whether the M flag is set,

E.>source the string identifier of the regular expression, the return result of the regular expression created using the literal and the constructor is consistent,

"Note" When you need to match a transfer character, the literal and the constructor are written differently,

var pattern =/\\/; matching slashes and using constructors will need to be written in the following way

var pattern = new RegExp (' \\\\ ');

      

for (var i = 0; i < i++) {

var pattern =/cat/g;

Alert (pattern.test (' I have a cat! ')); The first return is true, the second return is false, because, when you create the same regular expression using literals, you actually create only

An RegExp instance, after the first execution, the index position of the second start of cat was found to be 12

}

for (var i = 0;i < i++) {

var pattern = new RegExp (' Cat ', ' g ');

Alert (pattern.test (' I have a cat! ')); A new RegExp instance is created each time the constructor is used, so each time it returns true,

}

      [ECMAScript 5] explicitly stipulates that the use of regular expression literals must be the same as using the RegExp constructor, each time you must create a new instance,

2.exec and test methods,

var pattern =/.at/;

var match = pattern.exec (' I have a cat! '); The Exec method receives a string argument, which is a string that matches the pattern, and returns the Cat

This method is designed for capturing groups, returns an array containing the first occurrence information, returns NULL if there are no matches, returns an array, although an example of an array, but contains two other properties

Index and input

3. Properties of the constructor,

Input last string to match, (opera does not implement this property)

Lastmatch last occurrence, (opera does not implement this property)

Lastparen last matching capture array, (opera does not implement this property)

Leftcontext the text before lastmatch in the input string,

Rightcontext the text after the input string lastmatch,

Multiline Boolean value, which indicates whether all expressions are in multiline mode, (ie and opera do not implement this property),

4. Limitations of the model,

Compared to the Perl language, JavaScript Regular expressions are missing some of the features of advanced regular expressions, which are no longer one by one, because in our use, these are seldom used,

JavaScript Learning note "Four"

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.