zoukankan      html  css  js  c++  java
  • JavaScript 正则制表符,单词边界,去空格

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
        </head>
        <body>
            <script type="text/javascript">
                /*正则表达式中作为转义字符
                 * .表示任意字符,.来检查含.
                 * 注意,使用构造函数时,由于它的参数是一个字符串,
                 * 如果需要使用\,要写\
                 */
                 //. 制表符
    

    //          . 单个字符,除了换行和行结束符
    //            匹配单词边界
    //         B 不匹配单词边界
    //         w 单词字符,即字母数字下划线
    //         d 数字
    //         s 空格
    //          换行
    //          f 分页

    //                 ...
    //                 https://www.w3cschool.cn/jsref/jsref-obj-regexp.html
    
                 //.检查字符串中是否有单词child
                 var reg=/child/;
                 var reg1=/child/; //单词边界,注册用户名不允许空格常用
                 console.log(reg.test("Hello child")); //true
                 console.log(reg.test("Hello children"));//true,但是不符合题意
                 console.log(reg1.test("Hello children"));//false
                 
                 //.去空格,用户输入用户名会在前后不小心打入空格,智能去除
                 var str="        Hello W    ";
                     //前后全部空格 /^s*/  /s*$/
                 str=str.replace(/^s*|s*$/g,"");    
                 console.log(str);//Hello W
            </script>
        </body>
    </html>
  • 相关阅读:
    未来的计划和考虑
    jquery 常用的方法
    对于页面动态加载的元素事件无效的解决方案
    Myeclipse8.5中svn插件安装方法总结
    JS读RSS
    JAVA的RSS处理
    环境:win7+ie8 IE8的F12不起作用,原因如下:
    关闭和释放JDBC
    关于Eclipse无法生成class文件的问题
    JavaScript跨域总结与解决办法
  • 原文地址:https://www.cnblogs.com/wangdongwei/p/11251438.html
Copyright © 2011-2022 走看看