JS Study notes Second article

Source: Internet
Author: User

JS Note Finishing

1, Stringapi

A) uppercase and lowercase conversions: str.touppercase (); Str.tolowercase ();

b) Get the specified position character:

    1. Str[i]---> Countdown second str[str.length-2]
    2. Str.charat (i)
    3. 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");

    1. Each string is saved as an array in arr.
    2. Fixed routines: Cutting a string into a character array: var chars=str.split ("");
      1. 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

    1. \w ==>[0-9a-za-z];1 digits, letters, or underscores.
    2. \s = = "[null character] represents a null character: a space, tab character.
    3. . 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

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.