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>
  • 相关阅读:
    列表方块与格式与布局
    框架与样式表的基本概念
    表单
    内容容器,常用标签
    CSS3
    表单验证
    练习题
    document对象
    windows对象
    函数
  • 原文地址:https://www.cnblogs.com/wangdongwei/p/11251438.html
Copyright © 2011-2022 走看看