JavaScript_7(内置对象|正则表达式|数据类型转换)
Javascript内置对象目录
- Javascript String
- Number对象
- Javascript Array对象
- Javascript Boolean 对象【注意Boolean对象和Boolean值的区别】
- Javascript Date对象(日期)
6.Javascript Math对象(数学对象) - Javascript Reg Exp对象(正则表达式)
Reg Exp是正则表达式(regular expression)的简写;
Reg Exp:就是用来检索、判断文本数据的一种格式,在做数据效验的时候经常使用。
1. 创建格式(1):
var patt =new Reg Exp(pattern,modifiers);
创建格式(2):
var patt = /pattern/modifiers;【此方法不推荐使用】
注意:当使用构造函数创建正则对象时,需要常规的字符转义规则,【在前面加反斜杠 \】
2. 常用的正则表达式的字符串
- 校验数字的表达式
- 校验字符的表达式
- 特殊需求表达式
3. Reg Exp的常用操作方法
- test();搜索字符串指定的值,并根据结果返回真或者假
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>");
- exec(“被查找的数据值”) 方法检索字符串中的指定值。返回值是被找到的值。如果没有发现匹配,则返回 null。
var reg2 = new RegExp("134");
var text1 = "hello134sfdsworld"
var res2 = reg2.exec(text1);
if (res2 == null) {
document.write("<h1>不存在<>");
} else {
document.write("<h1>存在,结果是" + res2 + "</h1>"
}
近期所学内容总结
Javascript中的元素
1.变量 【var】
2.运算,表达式 【1.赋值 2.算数{++,–} 3.比较{===} 4.逻辑 5.typeof】
3.语句 【1.顺序结构 2.选择结构{switch{…break}} 3.循环{for/in} 4…】
4.函数{方法} 【function】
5.数据类型
6.对象 【1.内置对象 2.DOM 3 BOM】
JavaScript 类型转换—数据类型的转换
1. 数字转为字符串
- 全局方法String()将数字转为字符串型
转为对象型
var num1 = 100;
// 类型转换为对象型
var str1 = new String(num1); //String Object
document.write("<h1>str1===" + str1 + ",type==" + typeof str1 + "</h1>");
转为字符串型
var num1 = 100;
// 类型转换为对象型
var str1 = String(num1); //String value
// 既然是字符串,那么肯定有length这个属性,能检测出字符串的长度,就能证明该类型为字符串型
document.write("<h1>str1 ===" + str1 + ",type==" + typeof str1 + "</h1>");
- toString()方法
var num = 12;
console.log(num.toString());
后台输出黑色字体可以代表为字符串型
- 利用拼接技术 任何字符和字符串相加都等于字符串 这里可以添加空字符串 (隐式转换)
var num = 12;
console.log(num + "");
- .toFixed(小数的位数)把数字转换为字符串,结果的小数点后有指定位数的数字
var num1=100;
console.log(num1.toFixed(4));
2.将包含数字值的字符串转换为数字
- 全局方法 Number() 可以将字符串转换为数字。
转为数字型
var str1 = "25";
var num1 = Number(str1);
document.write("<h1>num1===" + num1 + ",type==" + typeof num1 + "</h1>")
new Number() 转为对象型
var str1 = "25";
var num1 = new Number(str1);
document.write("<h1>num1===" + num1 + ",type==" + typeof num1 + "</h1>")
- 包含数字值的字符串*1(隐式转换)
var str1 = "25";
var num1 = str1 * 1
document.write("<h1>num1===" + num1 + ",type==" + typeof num1 + "</h1>")
- 一元运算符转换
var str1 = "25";
var num1 = +str1;
document.write("<h1>num1===" + num1 + ",type==" + typeof num1 + "</h1>")
3. 将布尔值转换为字符串
- 全局方法 String() 可以将布尔值转换为字符串
var boo1 = true;
var str1 = String(boo1);
document.write("<h1>str1===" + str1 + ",typeof==" + typeof str1 + "</h1>")
- 使用toString()转换
var boo2 = true;
var str2 = boo2.toString();
document.write("<h1>str2===" + str2 + ",type==" + typeof str2 + "</h2>")
4.字符串转换为Boolean
- 转换为布尔值的对象
var str1 = "";
var boo1 = new Boolean(str1); //Boolean Object
document.write("<h1>boo1===" + boo1 + ",type==" + typeof boo1 + "</h1>");
- 转换为布尔值
var str1 = "";
var boo1 = Boolean(str1); //Boolean vaue
document.write("<h1>boo1===" + boo1 + ",type==" + typeof boo1 + "</h1>");
- 非空字符串被转换成true
var str2 = "王八伟";
var boo2 = Boolean(str2); //Boolean vaue
document.write("<h1>boo2===" + boo2 + ",type==" + typeof boo2 + "</h1>");
- 转换成布尔值的对象
var str2 = "王八伟";
var boo2=new Boolean(str2); //Boolean Object
document.write("<h1>boo2===" + boo2 + ",type==" + typeof boo2 + "</h1>");
- 值为false的字符串被转换成boolean值为true值,因为值为false的字符串也是非空字符串
var str3 = "false";
var boo3 = Boolean(str3); //Boolean vaue
document.write("<h1>boo3===" + boo3 + ",type==" + typeof boo3 + "</h1>");
-
数字型转为Boolean型
-
0转换为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>");
- 非0转换为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. Boolean转换为数字型
- true 被转换成数字1
var boo1=true; //true 被转换成数字1
//var num1=new Number(boo1);
var num1=Number(boo1);
document.write("<h1>num1==="+num1+",type=="+typeof num1+"</h1>");
- false被转换成0
var boo2=false; //false 被转换成数字0
//var num2=new Number(boo2);
var num2=Number(boo2);
document.write("<h1>num2==="+num2+",type=="+typeof num2+"</h1>");
- 乘1【*1】(隐式转换,若true为1,则结果等于1)
var boo1=true;
var num3=boo1*1;
document.write("<h1>num3==="+num3+",type=="+typeof num3+"</h1>");
(若false为0,则结果为0)
var boo2=false;
var num4=boo2*1;
document.write("<h1>num4==="+num4+",type=="+typeof num4+"</h1>");
6. 将日期转换为数字
- Number(变量);日期被转换成数字以后是从1970-1-1到当前日期的毫秒数
var date1=new Date();
//var num1=new Number(date1);
var num1= Number(date1);
document.write("<h1>num1==="+num1+",type=="+typeof num1+"</h1>");
- 日期方法 getTime() 也有相同的效果。
var date1=new Date();
var num2= date1.getTime();
document.write("<h1>num2==="+num2+",type=="+typeof num2+"</h1>");
7. 将数字转换为日期
- Date(变量)
var num1=160584453830;
//var date1=new Date(num1); //Object
var date1=Date(num1); //string
document.write("<h1>date1==="+date1+",type=="+typeof date1+"</h1>");
8. 将日期转换为字符串
- 全局方法 String() 可以将日期转换为字符串。
var date1=new Date();
//var str1=new String(date1);
var str1=String(date1);
document.write("<h1>str1==="+str1+",type=="+typeof str1+"</h1>");
- Date 方法 toString() 也有相同的效果。
var date1=new Date();
var str2=date1.toString();
document.write("<h1>str2==="+str2+",type=="+typeof str2+"</h1>");
9.将字符串转换为日期 注意:字符串中包含的是符合时间日期格式的字符串数据
var str1="2020-11-20 12:11:30";
var date1=new Date(str1);
document.write("<h1>date1==="+date1+",type=="+typeof date1+"</h1>");