• 周末复习了下正则


    //周末在家复习JS权威指南,真的是无底坑啊,好多基础的东西好多于啊,但是又不能不记住。
    <!
    DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title></title> </head> <script> window.onload = function() { var arrDiv = Array.prototype.slice.call(document.querySelectorAll("div")), len = arrDiv.length; while( len ) { len--; window[arrDiv.id] = arrDiv[len] }; }; </script> <body> //找出字符串中的所有数字; <script> var str = "sdfsdf2,2424sdfsdf00234"; var arrResult = []; for(var i= 0, len = str.length ;i < len ; i++) { if( Number(str[i]) != Number(str[i]) ) continue; arrResult.push( str[i] ); }; console.log("这个是用遍历弄出来的" , arrResult ); var arr = str.split(/[D]?/gi); arr = arr.filter(function(e){ return e !== ""; }); /*或者用这个 arr.ap(function(e){ return e !== ""; }); */ console.log("这个是用正则弄出来的",arr ); console.log("现在把字符串匹配;先来正则"); console.log( str.match(/d+/g) ); console.log("再来字符串连接!"); var arrResult = [], tmp = []; for(var i= 0, len = str.length ;i < len ; i++) { if( Number(str[i]) != Number(str[i]) ) continue; tmp.push( str[i] ); if( Number(str[i+1]) !== Number(str[i+1]) ) { arrResult.push( tmp.join("") ); tmp = []; }; }; console.log( arrResult ); </script> //过滤HTML标签; <div id="reg"> <span> sdfsd; <span>sdfsdfsdf</span> </span> </div> <script> var eEl = document.getElementById("reg"), reg = /<[^<]+>/gi; //如果和去掉换行符; console.log( eEl.innerHTML ); console.log( eEl.innerHTML.replace(reg,"") ); </script> //匹配中文; <input> <br> <input> <script> var aInpts = document.querySelectorAll("input"), regC = /[^u4e00-u9fa5]/gi; aInpts[0].addEventListener("input",function() { aInpts[1].value = this.value.replace(regC,function($$) { return ""; }); }, false); </script> <div id="www"> <span>输入网址:</span> <input id="www1"> <br> <span>匹配网址是否正确:</span> <div id="www2"></div> </div> <script> var reg = /[a-zA-Z]+://[^s]*/gi; www1.addEventListener("input",function() { www2.innerHTML = reg.test( this.value ); }); </script> <div id="匹配QQ号码"> <span>输入QQ号码:</span> <input id="qq_input"> <br>是否是正确的QQ号码; <span id="is_qq"></span> </div> <script> var regQ = /^[1-9][0-9]{4,10}$/g; qq_input.addEventListener("input",function() { is_qq.innerHTML = regQ.test(this.value) }); </script> <div id="date"> <span>日期里面把-变成.</span> <br> <input id="data1"> <br> <div id="data_result"></div> </div> <script> data1.addEventListener("input",function() { data_result.innerHTML = this.value.replace(/(?:d)-/g,function($1) { return $1.slice(0,1)+"."; }); }, false); </script> <div id=""> <span>排除所有数字:</span><br> <input id="str_input"> <div id="all_str"></div> </div> <script> str_input.addEventListener("input", function() { all_str.innerHTML = this.value.replace(/[^a-zA-Z]/g,function($1) { return ""; }); }, false); </script> <div> <span>重复的子项中间和两边添加-:</span><br> <input id="repeat"> <div id="repeat_div"></div> </div> <script> repeat.addEventListener("input", function() { repeat_div.innerHTML = this.value.replace(/(w+)([w]+)(1)/gi,function($all,$1,$2,$3){ return $1+"_"+$3; }); }, false); /* * * {4,7}最少4次,最多七次 * {4,}最少四次 * {4}刚好四次 * + === {1,} * * === {0,}其实跟通配符一样的啊有或者没有都是可以的 * ? === {0,1}这个的的批处理通配符也是0次或者一次的艺术 * * / </script> </body> </html>
  • 相关阅读:
    ASP.NET 配置log4net启用写错误日志功能
    jQuery formValidator表单验证插件
    jQuery Easy Validate Plugin
    https,https的本地测试环境搭建,asp.net结合https的代码实现,http网站转换成https网站之后遇到的问题
    Request 分别获取具有相同 name 属性表单元素值
    asp.net开源CMS推荐
    客户对账单本月欠款
    AR 客户本月回款
    应收帐款汇总
    采购订单关闭之PL/SQL实现方法
  • 原文地址:https://www.cnblogs.com/diligenceday/p/3972176.html
Copyright © 2020-2023  润新知