js高级--正则与异常

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        //1.对象定义方式
        var reg1= new RegExp(/abc/);
        //2.字面量方式【】{}
        var reg2 = /def/;
        //test()
        console.log(/./.test("\n"));//f    除了换行和空格的任意字符都可以
        console.log(/d/.test("asxdf"));//true    包含数字才true
        console.log(/D/.test("qwert111"));//false    没有数字才true
        console.log(/s/.test("     "));//t   
        console.log(/s/.test("ASDFG"));//f  有空白字符就true   S没有空白字符true  w只要有小写就true  
        console.log(/abc/.test("abc"));//t   
        console.log(/abc/.test("a"));//f
        console.log(/[abc]/.test("abc"));//t   只包含其中的任意一个就可以
        console.log(/[abc]/.test("abvvc"));//t
        console.log(/[^abc]/.test("abcde"));//t  包含abc和其他则true
        console.log((/[^abc]/.test("abc")));//f
        console.log((/[a-c]/).test("abcde"));//t  包含了abc范围内的任意一个则是true
        console.log((/[a-c0-9]/).test("a"));//t  包含abc或0-9的任意一个则是true
    </script>
</body>
</html>

正则涉及到的一些知识点与注释 和 异常涉及到的知识点

有关的代码详解我都有在后面添加注释

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        //抛出异常,此方法必须传入两个参数,否则报错。
        function fn1(param1,param2){
            if(param1===undefined||param2===undefined){
                //通过throw抛出异常。
                throw{
                    no:123,
                    msg:"两个参数必须都传入"
                }
            }
        }
        fn1(5,6);
        fn1(5);
        try {//把可能出现异常的代码放在try里,防止报错后下面的代码无法执行。
            fn1();
        } catch (error) {
            console.log(error.no,error.msg);
        }




        //异常传递机制
        function fn1(){
            fn2();
        }
        function fn2(){
            fn3();
        }
        function fn3(){
            throw new error("一个错误");
        }
        try {
            fn1();
        } catch (error) {
            console.log(error);
        }
        //try catch的完整写法,释放数据库连接等等
        finally{
            console.log("最终的结果");
        }

    </script>
</body>
</html>

js高级刚开始的内容还是很简单的,如果有看不懂的话可以在评论区问我哦。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值