JavaScript notes (v)

Source: Internet
Author: User
Tags date1 local time

Api:
Find keywords: 4 kinds:
1. Find the location of fixed keywords:
var i=str.indexof ("keywords", Fromi);
var i=str.lastindexof ("keywords", Fromi);
Problem: Regular is not supported
2. Determine if there are any keywords that contain and match the regex:
var i=str.search (REG);
Problem: Always start from 0 position, always only find the first one
3. Get all the keyword content:
var kwords=str.match (REG); Plus g
Problem: Unable to get the location of each keyword
4. Find the content of each keyword and find the location.
var arr=reg.exec (str);
Find the contents and location of a * keyword in str, saved in the array arr.
Complement: The Lastindex property of the Reg object:
Indicates the next time the match starts from the lastindex position
Default is 0
Principle:
1. Use Reg to match a keyword in str
2. Place the keyword content found in the first element of Arr, and record the location of the keyword in the Arr.index attribute.
3. Automatically adjusts the Lastindex property of the Reg to the current position.
return value:
If found: same match without G
arr[0]-> The complete keyword found this time
If there are groups in the regular:
Arr[1]-> Get sub-contents of 1th Group
Arr.index-> the location of this keyword
Not found: return null
How to use: Use loops to call exec repeatedly until NULL is returned.


Regular:
Greedy mode: The default match is as long as possible for a string of characters that matches the rule.
Lazy mode: Let the regular match only the shortest string that matches the rule.
Greed changes laziness:. *?. +?

Verify the format:
var bool=reg.test (str);
Verify that STR meets the format requirements for Reg.
Satisfies, returns True, does not satisfy return false
Emphasis: General validation is a complete match from beginning to end
Have to add ^, plus $.

2. Math: An API or constant that specializes in encapsulating mathematical calculations
When to use: just perform mathematical calculations
Create: Math cannot new!
API: All APIs are called directly with math
1. Rounding:
1. Rounding up: Remove an integer as long as the decimal point exceeds
Num=math.ceil (num)
2. Rounding down: Discard decimal Parts
Num=math.floor (num), can only be rounded down to a pure number
vs parseint (str):
Convert to Number
Remove the end of non-numeric characters
Discard a decimal part
3 Rounded rounding:
Num=math.round (num);
Emphasis: can only be rounded, can not set the number of decimal place
vs N.tofixed (d)
Parameter: round cannot specify decimal position, only rounding
ToFixed (d) can specify the number of decimal digits that can be rounded up by any number of decimal digits.
Return value: Round returns NUM, which can be directly involved in arithmetic calculations.
ToFixed (d) returns to STR, in the case of the + method calculation, to advance the type conversion.
Emphasis: Beware of rounding Errors:

2. exponentiation and Square:
Math.pow (base, Power)
MATH.SQRT (num) Open square

Math
Maximum and minimum values:
Math.max (value 1, value 2,...)
Math.min (value 1, value 2,...)
Emphasis: Max and min Do not support parameter of array type
Unable to get maximum value in array
Solution: Math.max.apply (Null,arr);
Gets the maximum value in the ARR array

Random number:
Math.random (): 0<=r<1
Formula: Go from any min~max to a random integer
parseint (Math.random () * (max-min+1) +min)
If from between 0~max
parseint (Math.random () * (max+1))

2.Date:
What is: encapsulates a time when the
provide operation time API
is used: whenever the time or date is saved, and when the time is manipulated.
Created: 3 kinds:
1. Create a Date object, save the current time:
var now=new date ();
emphasizes that now encapsulates the client local time.
2. Create Date object, save custom time: 2 kinds of
var date=
New Date ("Yyyy/mm/dd hh:mm:ss");
var date=
New Date (yyyy,mm-1,dd,hh,m M,SS);
Description: 1. If you do not care about seconds, you can omit
2. If you set the time with a number, the month is 1
3. Copy Date object:
Why? Because the date is calculated, the original date object is modified directly.
When to replicate: if you want to keep both the start time and the cutoff time, copy and then calculate.
How to copy:
var date1=new date (),
Var date2=new date (Date1.gettime ()),
Description,. GetTime () can omit
Principle: The Date object is actually saved in a huge number of milliseconds. Is the number of milliseconds since 0 o'clock January 1, 1970.

API:
Component: Fullyear Month Date Day
Hours Minutes Seconds Milliseconds
1. Each component has a pair of child getxxx/setxxx methods
where , the GetXXX method is responsible for getting the value of the specified component, the
Setxxx method is responsible for modifying the value of the specified component
Exception: Day of the week does not have Setday method
2. Name: Date and time of week, no s end
minutes seconds, there are S end
3. Value Range ( return value):
Only the day of the month from 1~31 end
The rest is from 0~-1 end
*month:0~11 need to fix: The month in the computer, less than the actual month 1
date:1~31 no correction
Day:0~6 With the fix: Sunday is the first day of the 0
hours:0~23 without correction
minutes/seconds:0~59 without modification

Date Calculation:
1. Two date objects can be subtracted: get millisecond difference
2. Add or subtract any component: 3 steps:
1. Take component: Var n=date.getxxx ();
2. Add and subtract: n+=x;
3. Put it back: Date.setxxx (n);
Description: 1. Setxxx method to automatically adjust the time
2. Setxxx method directly modify the original date
Simplified formula: date.setxxx (date.getxxx () +n)

Date goto string:
Date.togmtstring (); To China standard Time format
Date.tolocalestring (); Convert to local time format
Date.tolocaledatestring (); Retention date only
Date.tolocaletimestring (); Retention time only



JavaScript notes (v)

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.