JS Note Finishing
1, Stringapi
A) uppercase and lowercase conversions: str.touppercase (); Str.tolowercase ();
b) Get the specified position character:
- Str[i]---> Countdown second str[str.length-2]
- Str.charat (i)
- Str.charcodeat (i); return Unicode of characters?
c) Find the location of the keyword:
I.var i=str.indexof ("KWord" [, Starti]); return -1,starti default is 0 if not found
Ii var i=str.lastindexof ("KWord" [, Starti]); Find the first keyword that appears to the left of the Starti location Statri default is Length-1
d) Get substring:
I var substr=str.substring (starti[,endi+1]);
Ii var substr=str.substring (starti[,endi+1]); Negative parameters are not supported
Iii var substr=str.substring (Starti, take several);
Example: Get the user name and domain name.
Function () {
Var i=email.indexof ("@");
Var Username=email.slice (0,i);
Var Domain=email.slice (i+1);
Alert ("User name" +username);
Alert ("Domain name" +domain);
}
<button onclick= "Mail (Prompt (' Enter mailbox ')" > enter mailbox <button>
2. String API 2
A) delimited string: var arr=str.split ("delimiter");
- Each string is saved as an array in arr.
- Fixed routines: Cutting a string into a character array: var chars=str.split ("");
- Example: var a= "Hello";
var chars=a.split ("");
Console.log (chars);
Iii Pattern matching: 2 types
1.var I=str.search (/keword/i); only from the start position, Zhao first matches the keyword.
2.var Arr=str.match (/kword/ig);
A) The contents of all keywords are saved in arr
b) Arr.length indicates the number of matched keywords
c) * If not found, return to null*
d) * All must first determine whether is NULL, again processing! *
3.Var newstr=str.replace (mode, "replace content");
Examples:
var kword=/no/ig;
var reply=prompt ("input reply");
var arr=reply.match (KWord);
Reply=reply.replace (KWord, "* *");
alert (reply);
Alert ("Total substitution" + (arr?arr.length:0) + "place");
2. Regular Expressions:
A) An expression that specifically specifies the formatting rules for characters in a string format.
b) When to use: the regular expression is used whenever a string formatting rule is defined.
c) predefined character set: \d ==>[0-9] one digit
- \w ==>[0-9a-za-z];1 digits, letters, or underscores.
- \s = = "[null character] represents a null character: a space, tab character.
- . All other characters except line break.
d) quantifier: Specify the number of times a character set appears: 2 kinds
Definite quantity: 3 kinds: {Min,max}: Min bit, max bit
{min,}: Min bit at least
{n}: Must be N-bit
ID Number: At least 15 digits
2-digit number
Last digit or x x
The latter three bits are optional, but appear up to 1 times
\D{15} (\d{2}[0-9xx])?
Number of uncertainties: 3:
? ==>{0,1}: Optional, up to 1 times
*==>{0,}: Optional, more than an unlimited number of
+==>{1,}: At least once, more than an unlimited number of
Phone Number: +86 or 0086 optional, up to 1 times
Empty characters are optional, more than the number of unlimited
1
Choose one from 3,4,5,7,8
Must be 9 digits in the end.
(\+86|0086)? \s*1[34578]\d{9}
JS Study notes Second article