继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

regExp 基础教程

qaytix
关注TA
已关注
手记 104
粉丝 37
获赞 165
<script>
        /*匹配符号(- 中划线)*/
         let reg =  /^5-3$/;
     console.log(reg.test('5-3'));
        /*匹配符号(下划线 _ )*/
         let reg =  /^5_3$/;
     console.log(reg.test('5_3'));
</script>
<script>
    /*只能匹配一位数字,不是多个*/
    let reg = /^\d$/;
    console.log(reg.test('1'))
</script>
<script>
    /*匹配一或多数字*/
    let reg =/^\d+$/;
    console.log(reg.test('19845'));
</script>
<script>
    /*在这里点符号,转换了普通的字符,原来的点符号是,包含除了换行符号以外的数字*/
    let reg = /^2\.3$/;
    console.log(reg.test('2+3'));
</script>
<script>
    /*匹配18或19*/
    let reg = /^(18|19)$/;
    console.log(reg.test('18'));
</script>
<script>
    /*有四组,2便是同样匹配组的号码,第2个组和第三个组同样*/
    let reg = /^([a-z])|([0-9])\2([a-z])$/;
    console.log(reg.test('a55f'));
</script>
<script>
    let reg = /^(\w+)([\w-]*)([\w])$/;
    console.log(reg.test('asd----asd'));
</script>
<script>
    /*验证18-65之间的年龄*/
    let reg = /^((18|19)|([2-5]\d)|(6[0-5]))$/;
    console.log(reg.test('59'));
    /*

        18  68

        18  19
        20  59
        60  68

    */
    /*或运算符的意思是,可以出现任意一个,匹配任意的一个组件模块*/
    let reg = /^((18)|19)|([2-5]\d)|(6[0-8])$/;
   console.log(reg.test('57'))
</script>
<script>
    /*判断整数*/
/*
-12
12
12.5
*/
    let reg = /^(-)?(\d+)((\.\d+)?)$/;
    console.log(reg.test('12.65'));
</script>
<script type="text/javascript">
    /*
        oppo_A83@163.com
        oppo    (([a-z])|([A-Z])|(\d))+
        _       ((-|_)?)
        A83     (([a-z])|([A-Z])|(\d))+
        @       @{1}
        163     (([a-z])|([A-Z])|(\d))+
    */

    /*oppo_A83             let reg = /^(([a-z])|([A-Z])|(\d))+((-|_)?)$/;*/
    /*oppo_A83             let reg = /^(([a-z])|([A-Z])|(\d))+((-|_)?)((([a-z])|([A-Z])|(\d))+)?
    /*oppo_A83@            let reg = /^(([a-z])|([A-Z])|(\d))+((-|_)?)(([a-z])|([A-Z])|(\d))+@{1}$/;*/
    /*oppo_A83@163         let reg = /^(([a-z])|([A-Z])|(\d))+((-|_)?)(([a-z])|([A-Z])|(\d))+@{1}(([a-z])|([A-Z])|(\d))+$/;*/
    /*oppo_A83@163        let reg = /^(([a-z])|([A-Z])|(\d))+((-|_)?)(([a-z])|([A-Z])|(\d))+@{1}(([a-z])|([A-Z])|(\d))+(\.)(([a-z])|([A-Z])|(\d))+$/;*/
    let reg = /^(([a-z])|([A-Z])|(\d))+((-|_)?)((([a-z])|([A-Z])|(\d))+)?@{1}(([a-z])|([A-Z])|(\d))+(\.)(([a-z])|([A-Z])|(\d))+$/;
    console.log(reg.test('oppo_A83@163.com'));
</script>
<script>
    /*
    *
    * 1959   2018
    *
    * 1959   1999
    * 2000   2009
    * 2010   2018
    *
    * */

    let reg=/^(19[5-9]9)|(200\d)|(201[0-8])$/;
    console.log(reg.test('2015'));

</script>
打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP