Summary of JavaScript basic video tutorial (Chapter 081-090)

Keywords: Javascript Attribute encoding

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>081-090 Chapter summary</title>
</head>
<body>
    
<pre>
081. Date object
//Using Date object to represent a time in JS
</pre>
<script type="text/javascript">
    console.log("--081--");
    //If you use the constructor directly to create a Date Object, is encapsulated as the execution time of the current code
    var d01 = new Date();
    console.log(d01)
    //Create a specified time object
    //You need to pass a string representing time as a parameter in the constructor
    //Format month of date/day/Year time:branch:second
    var d02 = new Date("2/18/2011 11:10:30")
    console.log(d02)
    // getDate() - Get the current date object
    var curDate = d02.getDate()
    console.log(curDate)
    // getDay() - What day of the week to get the current date object - Will return a 0-6 0 for Sunday 1 for Monday
    var curDay = d01.getDay()
    console.log("Today is week."+curDay)
    //getMonth() Get the month of the current time object -Will return a 0-11 0 for January 11 for December
    var curMonth = d01.getMonth()
    console.log("This month is"+(curMonth+1)+"month")
    // getFullYear() - Gets the year of the current date object
    var curYear = d01.getFullYear()
    console.log("This year is"+curYear+"year")
    /*
     * getTime()
     *  - Get the timestamp of the current date object
     *  - The time stamp refers to the time stamp from January 1, 1970, 0:00:00, GMT
     *      The number of milliseconds taken to the current date (1 second = 1000 milliseconds)
     *  - The underlying computer uses time stamps when saving time
     */
    var getTime = d01.getTime()
    console.log(getTime)
    console.log( getTime/1000/60/60/24/365 )
    //Using timestamps to test the performance of code execution
    //Get the current timestamp
    var start = Date.now()
    for(var i=0 ; i<30 ; i++){
        console.log(i)
    }
    var end = Date.now()
    console.log("Code executed"+(end-start)+"Millisecond");
</script>

<pre>
082. Math
- Math Unlike other objects, it is not a constructor,
//It belongs to a tool class and does not need to create objects. It encapsulates the attributes and methods related to mathematical operations
Math Object attribute
1. Math.PI Represented pi
Math Object method
1. Math.abs(x) Returns the absolute value of a number
2. Math.ceil(x) Round up logarithm
3. Math.floor(x) Round down logarithm
4. Math.round(x) You can round a number
5. Math.max(x,y) Return x and y Highest value in
6. Math.min(x,y) Return x and y Lowest value in
7. Math.pow(x,y) Return x Of y pow
8. Math.sqrt(x) Return the square root of the number
9. Math.random() Return to 0 ~ 1 Random number between(decimal)
</pre>
<div id="randomColor">Random colors</div>
<script type="text/javascript">
    console.log("--082--");
    var pi = Math.PI
    console.log(pi)
    var zNum1 = 5.1
    var zNum2 = 5.9
    var fNum1 = -5.1
    var fNum2 = -5.9
    var abs1 = Math.abs(zNum1)
    var abs2 = Math.abs(fNum1)
    console.log(abs1)
    console.log(abs2)
    
    console.log("-------")
    var ceil1 = Math.ceil(zNum1)
    var ceil2 = Math.ceil(zNum2)
    var ceil3 = Math.ceil(fNum1)
    var ceil4 = Math.ceil(fNum2)
    console.log(ceil1) // 6
    console.log(ceil2) // 6
    console.log(ceil3) // -5
    console.log(ceil4) // -5
    
    var floor1 = Math.floor(zNum1)
    var floor2 = Math.floor(zNum2)
    var floor3 = Math.floor(fNum1)
    var floor4 = Math.floor(fNum2)
    console.log(floor1) // 5
    console.log(floor2) // 5
    console.log(floor3) // -6
    console.log(floor4) // -6
    
    var round1 = Math.round(zNum1)
    var round2 = Math.round(zNum2)
    var round3 = Math.round(fNum1)
    var round4 = Math.round(fNum2)
    console.log(round1) // 5
    console.log(round2) // 6
    console.log(round3) // -5
    console.log(round4) // -6
    
    var max1 = Math.max(1,2,3,10,18)
    var max2 = Math.max(-1,-2.1,-2.9,-3,-10,-18)
    console.log(max1) // 18
    console.log(max2) // -1
    
    var min1 = Math.min(1,2,3,10,18)
    var min2 = Math.min(-1,-2.1,-2.9,-3,-10,-18)
    console.log(min1) // 1
    console.log(min2) // -18
    
    var pow = Math.pow(2,4)
    console.log(pow) // 16
    
    var sqrt = Math.sqrt(2)
    console.log(sqrt) // 1.414
    
    var random1 = Math.random()
    console.log(random1)
    // Generates a random integer containing the lower limit but not the upper limit
    function randomNoUpp(lower,upper){
        return Math.floor( Math.random()*(upper-lower) + lower )
    }
    // Generates a random integer containing the lower and upper limit values
    function random(lower,upper){
        return Math.floor( Math.random()*(upper-lower+1) + lower )
    }
    var random2 = randomNoUpp(1,10)
    var random3 = random(1,10)
    console.log(random2)
    console.log(random3)
    // Expand random color values
    function randomColor(){
        var r =random(0,255),g =random(0,255),b =random(0,255)
        var result = "rgb("+r+","+g+","+b+"0"
        return result
    }
    var random_Color = document.getElementById("randomColor")
    random_Color.style.color = randomColor()
</script>

<pre>
083. Packaging class
//Basic types: Undefined, Null, Boolean, Number, String
//Reference type: Object, Array, Date, RegExp
JS There are only objects with properties and methods in. The original value has no properties and methods
//So we usually use the length property to find the length of a string?
//Three wrapper classes are provided for us in JS, through which we can convert data of basic data types into objects
String() - You can convert a basic data type string to String object
Number() - You can convert numbers of basic data types to Number object
Boolean() - Boolean values of basic data types can be converted to Boolean object
//But note: we will not use objects of basic data type in practical application. If we use objects of basic data type, some unexpected results may be brought when we make some comparisons
//Difference between reference type and basic packaging type:
//Their object lifecycles are different:
//Reference type: use new to create an instance of the reference type, which is always stored in memory when the execution data flow leaves the current scope.
 //Basic packing type: automatically create objects of basic packing type, and destroy them immediately after executing only one line of code.
</pre>
<script type="text/javascript">
    console.log("--083--");
    
    var num = new Number(3);
    var num2 = new Number(3);
    var str = new String("hello");
    var str2 = new String("hello");
    var bool = new Boolean(true);
    var bool2 = true;
    console.log(typeof num) //object
    console.log(num == num2) //false Compare the address of the object
    var b = new Boolean(false);
    if(b){
        console.log("I run.~~~"); // b Convert to for object Boolean,All for true
    }
    /*
     * Methods and properties can be added to objects, not to basic data types
     *  When we call properties and methods on values of some basic data types,
     *      The browser temporarily uses the wrapper class to convert it to an object, and then calls the object's properties and methods
     *      After calling, convert it to basic data type
     */
    var s = 123
    s = s.toString()
    console.log(s) // "123"
    console.log(typeof s) // string
    var s2 =456
    s2.hello="hello"
    console.log(s2) // 456
    console.log(s2.hello) //undefined
    
    var str="hello word";
    // 1.Create an object with the same value as the base type 
    //var str = new String("hello world");
    // 2.This object can call the method under the wrapper object and return the result to the long variable 
    //var long = str.length; 
    // 3.After that, the temporarily created object is destroyed
    //str = null;
    //because str No, length Property, so the above three steps will be performed automatically in the background before this step 
    var long = str.length; 
    console.log(long); // 10
    console.log(str.hello) //undefined
</script>

<pre>
084. Method of string
//At the bottom, the string is saved as an array of characters ["a", "B" and "C"]
length attribute-Can be used to get the length of a string
charAt()-Can return characters in a string at a specified position,Gets the specified character based on the index,Starting from 0
charCodeAt()-Gets the character encoding of the specified location character( Unicode Coding)
String.formCharCode()-Characters can be obtained according to character encoding
concat()-Can be used to connect two or more strings
indexof()
- This method can retrieve whether a string contains the specified content
- If the string contains this content, the first index is returned
- If the specified content is not found, return-1
- You can specify a second parameter that specifies where to start the search
lastIndexOf()
- Usage and indexOf()Same,
- The difference is indexOf It's from the front to the back,
- and lastIndexOf It's back to front
- You can also specify where to start finding
slice(x,y)
- The specified content can be intercepted from the string
- It will not affect the original string, but will intercept to the content return
- Parameters:
//First, index of start position (including start position)
//Second, the index of the end position (excluding the end position)
- If the second parameter is omitted, all the
- You can also pass a negative number as a parameter, which will be calculated from the back
substring(x,y)- Can be used to intercept a string, can slice()Similar
- Parameters:
- First: index of the start intercept location (including the start location)
- Second: index of end position (excluding end position)
- The difference is that this method does not accept negative values as parameters,
//If a negative value is passed, 0 is used by default
- It also automatically adjusts the position of the parameter. If the second parameter is smaller than the first, it will exchange automatically
substr(x,y)-Used to intercept strings
- Parameters:
1.Index of intercept start position
2.Cut length
split()-You can split a string into an array
- Parameters:
- A string is required as a parameter, and the array will be split according to the string
//If you pass an empty string as an argument, each character is split into an element in the array
toUpperCase()-Converts a string to uppercase and returns
toLowerCase()-Converts a string to lowercase and returns
</pre>
<script type="text/javascript">
    console.log("--084--");
    var baseStr = "aBcdefG"
    var baseStr2 = "aabbcdcc"
    var strLength = baseStr.length
    console.log("strLength=" + strLength) //7
    var charAt = baseStr.charAt(3) 
    console.log("charAt=" + charAt) // d
    var charCodeAt = baseStr.charCodeAt(0) 
    console.log("charCodeAt=" + charCodeAt) //97
    var formCharCode = String.fromCharCode(97)
    console.log("formCharCode=" + formCharCode) //a
    var concat = baseStr.concat("hijklmn","opqist")
    console.log("concat=" + concat) //aBcdefGhijklmnopqist
    var indexOf1 = baseStr2.indexOf("c")
    var indexOf2 = baseStr2.indexOf("e")
    console.log("indexOf1=" + indexOf1) // 4
    console.log("indexOf2=" + indexOf2) // -1
    var lastIndexOf1 = baseStr2.lastIndexOf("c",5)
    var lastIndexOf2 = baseStr2.lastIndexOf("e")
    console.log("lastIndexOf1=" + lastIndexOf1) // 4
    console.log("lastIndexOf2=" + lastIndexOf2) // -1
    var slice = baseStr.slice(1,3)
    var slice2 = baseStr.slice(1,-1)
    console.log("slice=" + slice) // Bc
    console.log("slice2=" + slice2) // Bcdef
    var substring = baseStr.substring(1,3)
    var substring2 = baseStr.substring(3,-3)
    console.log("substring=" + substring) // Bc
    console.log("substring2=" + substring2) // aBc
    var substr = baseStr.substr(1,3)
    var substr2 = baseStr.substr(3,0)
    console.log("substr=" + substr) // Bcd
    console.log("substr2=" + substr2) // empty
    console.log(typeof substr2) // string
    var split =baseStr.split("d")
    console.log("split=" + split) // aBc,efG
    console.log(typeof split) // object
    console.log(Array.isArray(split)) //true
    var toUpperCase =baseStr.toUpperCase()
    console.log("toUpperCase=" + toUpperCase) // ABCEFG
    var toLowerCase =baseStr.toLowerCase()
    console.log("toLowerCase=" + toLowerCase) // abcefg
</script>

<pre>
085. Introduction to regular expressions
//Regular Expression (English: Regular Expression, often abbreviated as regex, regexp or RE in code)
//Use a single string to describe and match a series of string search patterns that conform to a syntax rule.
//Search mode can be used for text search and text substitution.
//Regular expressions are search patterns formed by a sequence of characters.
//When you search for data in text, you can use search patterns to describe what you want to query.
//A regular expression can be a simple character, or a more complex pattern.
//Regular expressions can be used for all text search and text substitution operations.
</pre>
<script type="text/javascript">
    console.log("--085--");
    //Creating regular expression objects
    /*
     * Syntax:
     *  var Variable = new RegExp("regular expression", "match pattern");
     *  Use typeof to check regular objects and return objects
     *  var reg = new RegExp("a"); This regular expression can be used to check whether a string contains a
     *  You can pass a match pattern as the second parameter in the constructor,
     *  Could be
     * i ignore case
     * g To perform a global match, in short, is to find all the matches, not stop after finding the first one 
     * m Multi row lookup
     */
    var reg = new RegExp("a","g");
    var str = "A";
    /*
     * Regular expression method:
     *  test()
     * - This method can be used to check whether a string conforms to the rules of regular expression. If it conforms, it returns true, otherwise it returns false
     */
    var result = reg.test(str);
    console.log(result); //false
    console.log(reg.test("AcAAAA")); //false
    console.log(reg.test("AcAAAa")); //true
</script>

<pre>
086. regular grammars
//Use literals to create regular expressions
//Syntax: var variable = / regular expression / matching pattern
//Note: / / there must be no spaces before and after the expression
//It's easier to create in a literal way
//More flexibility with constructors
</pre>
<script type="text/javascript">
    console.log("--086--");
    // var reg = new RegExp("a","g");
    var reg = /a/g
    console.log(typeof reg) // object
    console.log(reg.test("abc")) //true
    // Use | To express or mean
    reg = /a|b|c/;
    console.log(reg.test("ab")) //true
    console.log(reg.test("A")) //false
    console.log(reg.test("de")) //false
    // Create a regular expression to check for letters in a string
    /*
     * []It's also a or relationship
     * [ab] == a|b
     * [a-z] Any lowercase letter
     * [A-Z] Any capital letter
     * [A-z] Any letter
     * [0-9] Arbitrary number
     */
    var reg = /[A-z]/
    console.log(reg.test("de")) //true
    // Check if a string contains abc or adc or aec 
    var reg = /a[bde]c/
    // [^...] The expression is used to find any character that is not between square brackets. One character that is not between square brackets is true. 
    reg = /[^ab]/;
    console.log(reg.test("de")) //true
    console.log(reg.test("ab")) //false
    reg = /[^0-9]/;
    console.log(reg.test("ab")) //true
    console.log(reg.test("123aaa")) //true //One is not true immediately
    console.log(reg.test("123")) //false
    console.log(reg.test("abs")) //true
</script>

<pre>
087. String and regular related methods
split()
- You can split a string into an array
- Method can pass a regular expression as a parameter, so the method will split the string according to the regular expression
- This method will all interpolate even if it does not specify a global match
- If the separator is at the head and tail of the string, add the value above the head and tail
search()
- Can search string for specified content
- If the specified content is searched, the index that appears for the first time will be returned. If the index is not searched, the index that appears for the first time will be returned-1
- It can take a regular expression as a parameter, and then retrieve the string according to the regular expression
- serach()Only the first one will be found, even if the global match is set, it is useless
match()
- According to the regular expression, the qualified content can be extracted from a string
- By default, our match Only the first content that meets the requirements will be found, and the retrieval will be stopped after it is found
//We can set the regular expression as a global match pattern, so that we can match everything
//Multiple matching patterns can be set for a regular expression, and the order does not matter
- match()The matching content will be encapsulated in an array and returned, even if only one result is found
replace(x,y)
- You can replace the specified content in the string with the new content
- Parameters:
1.The replaced content can accept a regular expression as a parameter
2.New content
- Only the first will be replaced by default
</pre>
<script type="text/javascript">
    console.log("087");
    var regStr = "a1a2b3c4d5f"
    var splitResult = regStr.split(/[A-z]/)
    console.log(splitResult) // ["", "1", "2", "3", "4", "5", ""]
    var searchResult = regStr.search(/[A-z]/)
    console.log(searchResult) // 0
    var matchResult = regStr.match(/[A-z]/g)
    console.log(matchResult) // ["a", "a", "b", "c", "d", "f"]
    var replaceResult = regStr.replace(/[A-z]/g , '*')
    console.log(replaceResult) // *1*2*3*4*5*
    console.log(regStr) // a1a2b3c4d5f
</script>

<pre>
088. regular expression syntax 
//Classifier
- You can set the number of times a content appears by quantifiers
- A quantifier only works on one of its preceding contents 
//Greedy quantifier n + is matched one by one from the back to the front, only once, and it is useless to set the whole situation
//Inert quantifier n +? Match from front to back, set global useful
,Dominant classifier(js I won't support it)n++  Match entire string
</pre>
<script type="text/javascript">
    console.log("088");
    // {n, m} Match previous at least n second,But not more than m second 
    // {n, } Match previous n second,Or many times 
    // {n} Match previous exactly n second 
    // ? Match previous item 0 or 1 times,That is to say, the former is optional. Equivalent to {0, 1} 
    // + Match previous item 1 or more times,Equivalent to{1,} 
    // * Match previous item 0 or more times.Equivalent to{0,}
    
    //Check if a string contains aaa
    var regExp = /a{3}/
    // ababab
    var regExp = /(ab){3}/
    // abc ,abbc ,abbbc
    reg = /ab{1,3}c/;
    console.log( reg.test("abc") ) //true
    console.log( reg.test("abbc") ) //true
    console.log( reg.test("abbbc") ) //true
    console.log( reg.test("abbbbc") ) //false
    //Check whether the a Start
    // ^ Beginning of expression
    // $ End of expression
    //If you use both in regular expressions^ $The string must fully conform to the regular expression
    reg = /^a$/
    console.log(reg.test("a")) // true
    console.log(reg.test("aa")) // false
    // Create a regular expression to check whether a string is a legal phone number
    var regMobile = /^1[3-9][0-9]{9}$/
    console.log(regMobile.test("11000000000")) //false
    console.log(regMobile.test("11000000000sss")) //false
    console.log(regMobile.test("15111111111")) //true
</script>

<pre>
089. Regular expression syntax 2
//Check whether a string contains
. Represents any character
 //Use \ as escape character in regular expression
\. To represent.
\\ Express\
//Note: when using a constructor, because its argument is a string, and \ is the escape character in the string,
//If you want to use \, you need to use \ \ instead
</pre>
<script type="text/javascript">
    console.log("089");
    var reg = /\./;
    reg = /\\/;
    reg = new RegExp("\\.");
    reg = new RegExp("\\\\");
    // [...] Any character in parentheses
    // [^...] Any character not in parentheses
    // . Any character except line feed,Equivalent to[^\n]
    // \w Any single character, Equivalent to[A-z0-9_]
    // \W Any non single character,Equivalent to[^A-z0-9_]
    // \s Any whitespace,Equivalent to[\ t \ n \ r \ f \ v]
    // \S Any non blank character,Equivalent to[^\ t \ n \ r \ f \ v]
    // \d Any number,Equivalent to[0-9]
    // \D Any character other than a number,Equivalent to[^0-9]
    // \b Word boundary
    // \B Except for word boundaries
    reg = /\w/;
    reg = /\W/;
    reg = /\d/;
    reg = /\D/;
    reg = /\s/;
    reg = /\S/;
    // Create a regular expression to check for words in a string child
    var regChild = /\bchild\b/
    console.log(regChild.test("hellochild")) // false
    console.log(regChild.test("hello childddd")) // false
    console.log(regChild.test("hello child")) // true
    // Remove blank space
    var str = "  sss      he      llo            fff  ";
    str = str.replace( /^\s*|\s*$/g ,'') //Go to the head and tail
    str = str.replace( /\s{2,}/g ,' ') // Go to the middle
    console.log(str)
</script>

<pre>
090. Regularity of mail
</pre>
<script type="text/javascript">
    console.log("090");
    // 01asQQ.ss_fdsa@01asQQ.com.cn
    // Any alphanumeric underline    .Any alphanumeric underline  @   Any alphanumeric     .Any letter (2-5 Position)   .Any letter (2-5 Position)
    var regMail = /^\w{3,}(\.|\w+)*@[A-z0-9]+(\.[A-z]{2,5}){1,2}$/
    console.log(regMail.test("as_.wfsfeas@sfsd5.com.cn")) //true
</script>

</body>
</html>

 

All basic course links:

 1. Summary of JavaScript basic video tutorial (Chapter 001-010)           2. Summary of JavaScript basic video tutorial (Chapter 011-020)          3. Summary of JavaScript basic video tutorial (Chapter 021-030)        4. Summary of JavaScript basic video tutorial (Chapter 031-040)

5. Summary of JavaScript basic video tutorial (Chapter 041-050)           6. Summary of JavaScript basic video tutorial (Chapter 051-060)         7. Summary of JavaScript basic video tutorial (Chapter 061-070)        8. Summary of JavaScript basic video tutorial (Chapter 071-080)

9. Summary of JavaScript basic video tutorial (Chapter 081-090) 10. JavaScript basic video tutorial summary (chapters 091-100) 11. JavaScript basic video tutorial summary (chapters 101-110) 12. JavaScript basic video tutorial summary (chapters 111-120)

13. JavaScript basic video tutorial summary (chapters 121-130) · 14. JavaScript basic video tutorial summary (chapters 131-140)

In addition, please pay attention to my Sina micro-blog

Posted by wrathican on Mon, 02 Dec 2019 01:53:19 -0800