JavaScript正则制表符,单词边界,去空格

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

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

//                 ...
//                 https://www.w3cschool.cn/jsref/jsref-obj-regexp.html

             //.检查字符串中是否有单词child
             var reg=/child/;
             var reg1=/\bchild\b/; //单词边界,注册用户名不允许空格常用
             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>

 

原文链接:https://www.cnblogs.com/wangdongwei/p/11251438.html
本文来源 爱码网,其版权均为 原网址 所有 与本站无关,文章内容系作者个人观点,不代表 本站 对观点赞同或支持。如需转载,请注明文章来源。

© 版权声明

相关文章