1, for the generator function, is not very understanding, and then continued
2. JavaScript Fun _ Don't ask why
-
Do not use new number ()
, new Boolean ()
, new String ()
to create the wrapper object;
-
Use parseint ()
or parsefloat ()
to convert any type to number
;
-
To use string ()
to convert any type to string
, or to invoke the toString ()
method of an object directly;
-
Usually does not have to convert any type to Boolean
, because you can directly write if (MyVar) {...}
-
typeof
operator to determine the number
, boolean
, string
, function
and undefined
;
-
Judging Array
to use Array.isarray (arr)
;
-
Judge null
use MyVar = = = null
;
-
Determine if a global variable exists typeof Window.myvar = = = ' undefined '
;
-
Function internally to determine whether a variable exists with typeof MyVar = = ' undefined '
.
1 // SyntaxError
// ' 123 ', attention is two points! // ' 123 '
2, for the Date object, a place where the father of the hole is the JavaScript month range is represented by an integer 0~11, which represents 0
January, 1
February ..., so to say June, we passed in 5
!
1 var New Date (); 2 if (Today.getmonth () = = = 2 && today.getdate ()= = =3 alert (' Dear, I've booked dinner, I'll meet you at the restaurant 6 o'clock in the evening! '); 4 }
The above code, really is the rhythm of solitary born ah ... Because it matches the March 14 (the pit is everywhere).
3, REGEXP (regular expression)
.
Can match any character, so:
‘js.‘
Can match ‘jsp‘
, ‘jss‘
,‘js!‘
To match a variable-length character, in a regular expression, with a representation of *
any character (including 0), with a representation of +
at least one character, representing ?
0 or 1 characters, with a representation of {n}
n characters, represented by {n,m}
n-m characters:
Advanced:
[0-9a-zA-Z\_]
Can match a number, letter, or underscore;
[0-9a-zA-Z\_]+
Can match a string of at least one number, letter, or underscore, for example, and ‘a100‘
‘0_Z‘
‘js2015‘
so on;
[a-zA-Z\_\$][0-9a-zA-Z\_\$]*
You can match a string consisting of a number, letter, or underscore, or $, which is the name of the variable allowed by JavaScript, by a letter or underscore, or $.
[a-zA-Z\_\$][0-9a-zA-Z\_\$]{0, 19}
More precisely limit the length of a variable to 1-20 characters (1 characters before + 19 characters later).
A|B
Can match A or B, so (J|j)ava(S|s)cript
you can match ‘JavaScript‘
, ‘Javascript‘
, ‘javaScript‘
or ‘javascript‘
.
^
Represents the beginning of a row, ^\d
indicating that a number must begin.
$
Represents the end of a line, indicating that it \d$
must end with a number.
You may have noticed it, but you can match it, js
‘jsp‘
but plus ^js$
it turns into an entire line match, it only matches ‘js‘
.
1 var re1 =/abc\-001/; 2 var New REGEXP (' abc\\-001 '); 3 4 // /abc\-001/ 5 // /abc\-001/
The method of the RegExp object test()
is used to test whether a given string meets the criteria
1 var re =/^\d{3}\-\d{3,8}$/; 2 // true 3 // false 4 // false
To slice a string:
1 ' A, b;; C // [' A ', ' B ', ' C ', ' d ']
grouping, greedy matching, global matching and searching
A. ^(\d{3})-(\d{3,8})$
Two groups are defined, and the area code and local numbers can be extracted directly from the matching string
exec()
After the match succeeds, the method returns one Array
, the first element is the entire string to which the regular expression matches, and the subsequent string represents the successful substring.
exec()
Method is returned when a match fails null
.
1 var re =/^ (\d{3})-(\d{3,8}) $/; 2 // [' 010-12345 ', ' 010 ', ' 12345 '] 3 // NULL
B. Regular matching is a greedy match by default, which is to match as many characters as possible
1 var re =/^ (\d+) (0*) $/; 2 // [' 102300 ', ' 102300 ', ']
Let \d+
a non-greedy match (that is, as few matches as possible) to the back of the 0
match out, add a ?
can let \d+
the use of non-greedy matching
1 var re =/^ (\d+?) (0*) $/; 2 // [' 102300 ', ' 1023 ', ' xx ']
C.javascript Regular expressions also have several special flags, most commonly g
, representing global matches, and global matching can execute methods multiple times exec()
to search for a matching string. When we specify a g
flag, each time it is run exec()
, the regular expression itself updates the lastIndex
property to indicate the last index to which it was last matched
1 vars = ' JavaScript, VBScript, JScript and ECMAScript ';2 varre=/[a-za-z]+script/G;3 4 //use global match:5Re.exec (s);//[' JavaScript ']6Re.lastindex;//Ten7 8Re.exec (s);//[' VBScript ']9Re.lastindex;// -Ten OneRe.exec (s);//[' JScript '] ARe.lastindex;// in - -Re.exec (s);//[' ECMAScript '] theRe.lastindex;// - - -Re.exec (s);//NULL until the end is still not matched to
The regular expression can also specify i
flags, which indicate that the case is ignored, the m
flag, indicating that a multiline match is performed
Example: matching email address:
1 varRe =/^[\da-za-z]+ ([. _-]\w+) *@ ([\da-za-z]+\.) +[a-za-z]{2,6}$/;2 3 varRe =/^< (. +?) >\s* ([\da-za-z]+[._-]?\[email protected][\da-za-z]+\.[ a-za-z]{2,6}) $/;//Group to get an email address part of the content4 5 //Test section:6 //Test:7 varr = re.exec (' <tom paris> [email protected] ');8 if(r = =NULL|| R.tostring ()!== [' <tom paris> [email protected] ', ' Tom Paris ', ' [email protected] '].tostring ()) {9Alert (' Test failed! '));Ten } One Else { AAlert (' Test succeeded! ')); -}
The fun of JavaScript--0220