JavaScript的基本语法(续)

九、数学对象

        1.创建:Math对象不用创建,直接使用!使用时直接调用方法,例:Math.方法名();

        2.常用方法:

        (1)random():返回0~1之间的随机数

        (2)max(a,b):返回a和b中的最大值

        (3)min(a,b):返回a和b中的最低值

        (4)ceil(x):对数X进行向上取整

        (5)floor(x):对数X进行向下取整

        (6)round(x):把数X四舍五入为最接近的整数

        (7)abs(x):返回X的绝对值

        3.属性:PI,圆周率

        var a=1.414,b=2.448;
        控制台测试
        console.log(Math.random());
        console.log(Math.max(a,b));
        console.log(Math.min(a,b));
        console.log(Math.ceil(a));
        console.log(Math.floor(b));
        console.log(Math.round(a));
        console.log(Math.abs(-8));
        console.log(Math.PI);

十、字符串对象

        1.创建:

        (1)var str1="abd";

        (2)var str2=new String("abd")

        2.常用方法:

        (1)获取字符串长度str1.length

        (2)是否以某个字符开头startsWith()

        (3)是否以某个字符结尾endWith()

        (4)截取字符串

                【1】substring(起始下标,结束角标)

                【2】substr(起始下标,截取长度)

        (5)根据某个字符拆分字符串 返回数组split()

        var str="I LOVE STUDY";
        console.log(str.length);
        console.log(str.startsWith("a"));
        console.log(str.endsWith("y"));
        console.log(str.substring(2,6));//含3,不含6
        console.log(str.substr(2,6));//含3,含6
        console.log(str.split(" "));

十一、正则表达式

        1.单个字符:例[a]、[b]、[a-zA-Z.-9]、[1-9]

        2.元字符:\d 表示数字

        3.量词字符:

                ?:表示出现0次或1次

                *:表示出现0次或多次

                +:表示出现1次或多次

                {m}:表示m的数量

                {m,n}:表示m=<数量<=n

                4.开始结束符号:

                        ^开始

                        $结束

                5.创建正则表达式对象:

                (1)var reg=new RegExp("正则表达式");

                (2)var reg=/正则表达式/;

                6.测试正则验证的方法:

                        test(参数):验证指定的字符串是否符合正则定义的规范    

        

        var res=/^[1][3-9]\d{9}$/;
        var phone="kjhqiudsffga";
        var phone1="";
        var phone2="24568793245";
        var phone3="14568793245";
        console.log(res.test(phone));
        console.log(res.test(phone1));
        console.log(res.test(phone2));
        console.log(res.test(phone3));

#学无止境#

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南枫知我意~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值