Javascript_7 (built-in object | regular expression | data type conversion)

JavaScript_7 (built-in object | regular expression | data type conversion)

Javascript built-in object directory

  1. Javascript String
  2. Number object
  3. Javascript Array object
  4. Javascript Boolean object [note the difference between Boolean objects and Boolean values]
  5. Javascript Date object (date)
    6.Javascript Math object (mathematical object)
  6. Javascript Reg Exp object (regular expression)

Reg Exp is short for regular expression;

Reg Exp: It is a format used to retrieve and judge text data, and is often used when doing data validation.

1. Create the format (1):
var patt =new Reg Exp(pattern,modifiers);

Create format (2):
var patt = /pattern/modifiers; [This method is not recommended]
Note: When using the constructor to create a regular object, regular character escaping rules are required, [prepend a backslash \]

2. Strings of commonly used regular expressions

  • check digit expression

    - an expression for the check character

- Special needs expressions

3. Common operation methods of Reg Exp

  • test(); searches for the value specified by the string and returns true or false according to the result
    var reg1 = new RegExp(/^(13[0-9]|14[57|]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/);
    var phoneNum = "100000";
    var res1 = reg1.test(phoneNum);
    document.write("<h1>" + res1 + "</h1>");

  • The exec("data value being looked for") method retrieves the specified value in the string. The return value is the value found. Returns null if no match is found.
    var reg2 = new RegExp("134");
    var text1 = "hello134sfdsworld"
    var res2 = reg2.exec(text1);
    if (res2 == null) {
      document.write("<h1>does not exist<>");
    } else {
      document.write("<h1>exists, the result is" + res2 + "</h1>"
    }

Summary of what has been learned recently

Elements in Javascript
1. Variable [var]
2. Operation, expression [1. Assignment 2. Arithmetic {++,–} 3. Comparison {===} 4. Logic 5.typeof]
3. Statement [1. Sequence structure 2. Selection structure {switch{…break}} 3. Loop {for/in} 4…]
4. Function {method} [function]
5. Data Types
6. Object [1. Built-in object 2.DOM 3 BOM]

JavaScript type conversion - data type conversion

1. Convert numbers to strings

  • The global method String() converts numbers to strings

convert to object

    var num1 = 100;
    // type conversion to object type
    var str1 = new String(num1); //String Object
    document.write("<h1>str1===" + str1 + ",type==" + typeof str1 + "</h1>");

convert to string

    var num1 = 100;
    // type conversion to object type
    var str1 = String(num1); //String value
    // Since it is a string, there must be an attribute of length, which can detect the length of the string and prove that the type is a string.
    document.write("<h1>str1 ===" + str1 + ",type==" + typeof str1 + "</h1>");

  • toString() method
    var num = 12;
    console.log(num.toString());

The background output black font can be represented as a string type

  • Using the concatenation technique, any character and string add up to a string. An empty string can be added here (implicit conversion)
    var num = 12;
    console.log(num + "");

  • .toFixed( number of decimal places) converts the number to a string, and the result has the specified number of digits after the decimal point
    var  num1=100;
    console.log(num1.toFixed(4));
    

2. Convert a string containing a numeric value to a number

  • The global method Number() converts a string to a number.

Convert to digital

var str1 = "25";
    var num1 = Number(str1);
    document.write("<h1>num1===" + num1 + ",type==" + typeof num1 + "</h1>")

new Number() to object type

    var str1 = "25";
    var num1 = new Number(str1);
    document.write("<h1>num1===" + num1 + ",type==" + typeof num1 + "</h1>")

  • String*1 containing numeric value (implicit conversion)
    var str1 = "25";
    var num1 = str1 * 1
    document.write("<h1>num1===" + num1 + ",type==" + typeof num1 + "</h1>")

  • Unary operator conversion
var str1 = "25";
    var num1 = +str1;
    document.write("<h1>num1===" + num1 + ",type==" + typeof num1 + "</h1>")

3. Convert boolean to string

  • The global method String() can convert a boolean value to a string
var boo1 = true;
    var str1 = String(boo1);
    document.write("<h1>str1===" + str1 + ",typeof==" + typeof str1 + "</h1>")

  • Convert using toString()
var boo2 = true;
    var str2 = boo2.toString();
    document.write("<h1>str2===" + str2 + ",type==" + typeof str2 + "</h2>")

4. Convert string to Boolean

  • object converted to boolean
    var str1 = "";
    var boo1 = new Boolean(str1); //Boolean Object
    document.write("<h1>boo1===" + boo1 + ",type==" + typeof boo1 + "</h1>");

  • convert to boolean
 var str1 = "";
    var boo1 = Boolean(str1); //Boolean vaue
    document.write("<h1>boo1===" + boo1 + ",type==" + typeof boo1 + "</h1>");

  • non-empty strings are converted to true
 var str2 = "Wang Bawei";
    var boo2 = Boolean(str2); //Boolean vaue
    document.write("<h1>boo2===" + boo2 + ",type==" + typeof boo2 + "</h1>");

  • object converted to boolean
var str2 = "Wang Bawei";
    var  boo2=new Boolean(str2);  //Boolean Object
    document.write("<h1>boo2===" + boo2 + ",type==" + typeof boo2 + "</h1>");

  • A string with a value of false is converted to a boolean with a value of true, because a string with a value of false is also a non-empty string
    var str3 = "false";
    var boo3 = Boolean(str3); //Boolean vaue
    document.write("<h1>boo3===" + boo3 + ",type==" + typeof boo3 + "</h1>");

  • Convert numeric to Boolean

  • 0 is converted to false

var  num1=0;
//var  boo1=new Boolean(num1);  //Boolean Object
var  boo1=Boolean(num1);  //Boolean vaue
document.write("<h1>boo1==="+boo1+",type=="+typeof boo1+"</h1>");

  • non-zero is converted to true
var  num2=123;
//var  boo1=new Boolean(num1);  //Boolean Object
var  boo2=Boolean(num2);  //Boolean value
document.write("<h1>boo2==="+boo2+",type=="+typeof boo2+"</h1>");

5. Convert Boolean to Number

  • true is converted to the number 1
var boo1=true;  //true is converted to the number 1
//var num1=new Number(boo1);
var num1=Number(boo1);
document.write("<h1>num1==="+num1+",type=="+typeof num1+"</h1>");

  • false is converted to 0
var boo2=false;  //false is converted to the number 0
//var num2=new Number(boo2);
var num2=Number(boo2);
document.write("<h1>num2==="+num2+",type=="+typeof num2+"</h1>");

  • Multiply by 1[*1] (implicit conversion, if true is 1, the result is equal to 1)
var boo1=true;
var num3=boo1*1;
document.write("<h1>num3==="+num3+",type=="+typeof num3+"</h1>");


(if false is 0, the result is 0)

var boo2=false;
var num4=boo2*1;  
document.write("<h1>num4==="+num4+",type=="+typeof num4+"</h1>");

6. Convert date to number

  • Number (variable); the number of milliseconds from 1970-1-1 to the current date after the date is converted to a number
var date1=new Date();
//var num1=new Number(date1);
var num1= Number(date1);
document.write("<h1>num1==="+num1+",type=="+typeof num1+"</h1>");

  • The date method getTime() also has the same effect.
var date1=new Date();
var num2= date1.getTime();
document.write("<h1>num2==="+num2+",type=="+typeof num2+"</h1>");

7. Convert numbers to dates

  • Date( variable)
var num1=160584453830;
//var date1=new Date(num1); //Object
var date1=Date(num1); //string
document.write("<h1>date1==="+date1+",type=="+typeof date1+"</h1>");

8. Convert date to string

  • The global method String() can convert dates to strings.
var date1=new Date();
//var str1=new String(date1); 
var str1=String(date1);
document.write("<h1>str1==="+str1+",type=="+typeof str1+"</h1>");

  • The Date method toString() also has the same effect.
var date1=new Date();
var str2=date1.toString();
document.write("<h1>str2==="+str2+",type=="+typeof str2+"</h1>");

9. Convert the string to a date Note: The string contains string data that conforms to the time and date format

var str1="2020-11-20 12:11:30";
var date1=new Date(str1);
document.write("<h1>date1==="+date1+",type=="+typeof date1+"</h1>");

Tags: Javascript

Posted by Beauchy on Fri, 06 May 2022 21:07:17 +0300