正则表达式

扫码查看
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <script>
            //需求一、验证手机号码是否正确,=>判断一个字符串是不是11位数字组成的.
            // var reg = /^1\d{10}$/g;
            // var str = "1234578944";
            // console.log(str.match(reg));
            // //g:全局匹配;是返回所有符合条件的子字符串(数组)
            // //没有g:返回一个符合条件的子字符串(类数组)
            // if(str.match(reg)){
            //     console.log("手机号码正确!")
            // }else{
            //     console.log("手机号码不正确!")
            // }
            //需求二:验证座机号码是否正确,xxxx-xxxxxxxx(特点:左边区号:3-4位数字;
            //右边:是电话号码6-8位数字)
            var reg = /^\d{3,4}-\d{6,8}$/g;
            var str = "2345-12313442 2345-1231312323";
            //reg.lastIndex = 0
            //exec匹配一个符合条件的子字符串,会记录匹配到的位置.
            console.log(reg.exec(str));
            //reg,lastIndex = 13
            console.log(reg.exec(str));
            //reg,lastIndex = 16
            console.log(reg.exec(str));
            console.log(reg.test(str));
            //g:全局匹配;是返回所有符合条件的子字符串(数组)
            //没有g:返回一个符合条件的子字符串(类数组)
            var newstr = str.replace(reg,'abc');
        </script>
    </body>
</html>
12-30 01:00
查看更多