The sixth day of learning JS

Source: Internet
Author: User

some knowledge review and notes learned today

1. Regular Expressions (REGEXP)

1.1 notation

/the contents of the regular expression/

Example:/^\w{6,10}\@[0-9a-z]{1,4}.com$/, this is a regular expression, simple validation of the mailbox format expression.

Let's explain the example below:

(1) ^: The beginning of the regular expression.

(2) $: The end of the regular expression.

(3) \w: Represents numbers, letters, and underscores.

(4) {6,10}: Curly braces represent the number of digits, meaning 6 to 10 bits, which means that the preceding \w is 6 to 10 bits.

(5) \@: The escape character "\" plus a special symbol means matching this character. If they are not special characters in the regular expression (such as "^" and "$", etc.), they can be used without the escape character.

(6) [0-9a-z]: curly braces represent the range, which represents any number from 0 to 9 and any letter from A to Z.

(7) {1,4}: Same as above {6,10}.

There are also some frequently used:

(8) \d: The expression is [0-9].

(9) \s: space

(Ten) \d: matches except for numbers.

\w: In contrast to \w, matches except numbers, letters, and underscores.

\s: In contrast to \s, matches except for spaces.

(13) +: Indicates 1 to more bits, {1,}

(14) *: Indicates 0 or more digits.

(15) ".": Indicates that all of the matches are matched.

1.2 Methods

(1) test (): Tests for matching objects.

An example of a matching month:
var month = "12";
var val =/0[1-9]|1[0-2]/; The month is 1-9 months and 10, 11, December, so there are 0 months or 1 months. "|" Represents or.
var result = val.test (month); Method of the regular expression test, testing the string variable month.
Console.log (result); The console will output true, and if you change month to 13, it will return false.

(2) EXEC (): Matches what shows what, returns an array where the matching results are stored. If no match is found, the return value is null.

var str = "23342addff#"
var reg =/[0-9a-z]\#/
var re = reg.exec (str);
Console.log (re); Display array [F #], connected to match, for example, the first five in the STR variable is a number, followed by five is the letter, the last is # if the reg is stored in the/[0-9]{5}\#/, the console display is null. Because the numbers and # are not directly connected, this will not match.

1.3 Applications

(1) Join (): Concatenate the strings in the array, and you can write the custom connector in parentheses.

var arr = ["Hello", "World", "!"];
var re = Arr.join ("$"); For the array arr is connected with the "$" symbol.
Console.log (re); Shows the hello$world$!.

(2) Split (): Split score Group

var str = "[email protected]";
var val =/[@-]/;
var re = Str.split (val); For string str, retrieves the symbols in the regular expression in the string and separates them in an array form.
Console.log (re); Show array["2015", "12", "20"]

2.toString ()

    The ToString () method converts a logical value to a string and returns the result.

Cases:

var num = 12343;
var str = num.tostring ();
Console.log (str); Showing 12343
Console.log (typeof (str));//Display string

  3. About closure issues

1. Function assignment has a return value
         var a = function () {   //Assign a function to a, now a is a function.
              return "Hello";   // The return value hello.   If this sentence is commented out, then the value of B below is undefined, because there is no return value, this function will not get any value even if B is executed. The following is written if no return value is in the case.
        }
         A (); //Call function a
         var b = A ();  //define B for the value returned by this function A.
        console.log (b);    //console will display Hello

2. Immediate function has return value
       var a = function () {            //a is assigned the return value of the immediate function, and a is not a function.    
             return "haha"
         } ()
        var b = a;  //So here A is directly equal to B, not a ().
         Console.log (b);   //display haha


3. The non-immediate function does not return a value.
             var a = function () {   //Assign a function to a, Now a is a function.
            }
        a (); //Call function a
         var b = A ();  //define B for the value returned by this function A.
         Console.log (b);  //display undefined because function A does not return a value.

4. Immediate function does not return a value case
var a = function () {//a is assigned to the return value of the immediate function, and a is not a function.
}()
var B = A; So here A is directly equal to B, not a ().
Console.log (b); The undefined is also shown here.

    In summary, if the function does not return a value, whether the function is immediately executed or not immediately execute the function is not a value, only executes the code in the function body, non-immediate functions need to call the function to execute. If a function has a return value, if a non-immediate function is given a variable B, then B equals the return value of the function, and if the function is executed immediately, then B is immediately executing the function's return value.

The sixth day of learning JS

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.