网页制作之JavaScript正则

  • test 判断字符串是否符合规定的正则

        rep = /\d+/;
        rep.test("asgdgsahiu526")
        #   true
    
        rep = /^\d+$/;
        rep.test("agduya6768bhjsadgiak");
        #   false
    
  • exec 获取字符串中符合规定的正则

        rep = /\d+/;
        var str = "ashda 312 bdhsah_398";
        rep.exec(str);
        #   312
        rep = /\bhello(\w*)\b/;
        var str = "ada hellohi adsad";
        rep.exec(str);
        #   ["hellohi","hi"]
    
        rep = /\bhello(\w*)\b/g;    全局搜索模式
        var str = "ada hellohi adsad hello! hello";
        rep.exec(str);
        #   ["hellohi","hi"]
        #   ["hello",""]
        #   ["hello",""]
        #   null
    
  • 多行匹配

       默认就是多行匹配
       ^(开始符)+内容+$(终止符)
       rep = /\bhello(\w*)\b/g;    全局搜索模式
       var str = "ada hellohi adsad\n hellowe asdha hell";
       rep.exec(str);
       #   ["hellohi","hi"]
       #   null
       rep = /\bhello(\w*)\b/gm;    全局搜索模式+多行匹配
       var str = "ada hellohi adsad\n hellowe asdha hell";
       rep.exec(str);
       #   ["hellohi","hi"]
       #   ["hellowe","we"]
       #   null
    
  • 登录注册验证

    1.  先验知识
        默认事件先执行
            CheckBox
        自定义先执行
            a
            submit
            。。。
    
        <form>
            <input type="text"/>
            <input type="password"/>
            <input type="submit"/>
        <!--        创建一个CheckBox,初始状态checked为false-->
            <input type="checkbox"/>
        </form>
        <script src="jquery-1.12.4.js"></script>
        <script>
            // 自定义一个点击事件,点击的时候向console发送当前的checked状态
            $(":checkbox").click(function () {
                // 获取checked当前状态
                var v = $(this).prop("checked");
                // 发送到console
                console.log(v);
            })
            $(":submit").click(function () {
                $(":text,:password").each(function () {
                    // ...
                    return false;
                });
                return false;
            });
        </script>
    2.  验证
        前端:JS验证
        <form>
            <input type="submit"/>
        </form>
        <script src="jquery-1.12.4.js"></script>
        <script>
            $(":submit").click(function () {
                $(":text,:password").each(function () {
                    // ...
                    return false;
                });
                return false;
            });
        </script>
        后端:Python实现
        业务处理
        。。。
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值