一、备选字符集

  1. var str ="上海,上天,上哪里去";
  2. var reg =/上[海天]/g
  3. console.log(str.replace(reg,"*")); // *,*,上哪里去
  4. var reg = /[上天哪]/g
  5. var str = "上海,上天,上哪里"
  6. console.log(str.replace(reg,"*")); // *海,**,**里
  7. var str = "我去,我草,你这游戏打的太牛逼了"
  8. var reg = /[去草]|牛逼/g
  9. console.log(str.replace(reg,"*")); // 我*,我*,你这游戏打的太*了
  10. var res = "ajkdgkbcj128973489"
  11. var reg = /[0-9]/g
  12. console.log(res.replace(reg,"*")); // ajkdgkbcj*********
  13. var str = "hello"
  14. var reg = /[el]o/g
  15. console.log(str.replace(reg,"*")); // hel*

二、转义字符

  1. var str = "https://www.baidu.com"
  2. var str2 = "http://www.baidu.com"
  3. var reg = /(http|https):\/\/[w]{3}\.baidu\.com/
  4. console.log(reg.test(str));
  5. console.log(reg.test(str2));

三、指定匹配的位置

  1. // 电话
  2. var num = "13897932473123"
  3. var reg = /^1[3-9]\d{9}$/
  4. console.log(reg.test(num));
  1. <input type="text" id="input">
  2. <script>
  3. /* 去除开头结尾的空格 */
  4. var res = []
  5. var reg = /^\s+|\s+$/g;
  6. var input = document.getElementById("input")
  7. input.onkeydown = function(event){
  8. if(event.keyCode == 13){
  9. var str = this.value
  10. res.push(str.replace(reg,""))
  11. console.log(res);
  12. }
  13. }
  14. </script>

四、非

  1. var str = "hello210874"
  2. var reg = /[^a-z]/g
  3. console.log(str.replace(reg,"")); // hello
  4. var str = "<p>我是p标签</p>。<div>我是div</div>"
  5. var reg = /<[^<>]+>/g
  6. console.log(str.replace(reg,"")); // 我是p标签。我是div
  7. var str = "https://movie.douban.com/subject/1292052/"
  8. var reg = /[^\d+]/g
  9. console.log(str.replace(reg,"")); // 1292052

五、邮箱验证

  1. <input type="text" id="email" placeholder="请输入邮箱">
  2. <script>
  3. var e=document.getElementById("email")
  4. e.onkeyup=function(event){
  5. if(event.keyCode==13){
  6. var value=this.value.trim();
  7. var reg=/^[\w]+@[0-9a-z]+\.[a-z]+$/;
  8. console.log(reg.test(value))
  9. }
  10. }
  11. </script>

六、密码验证

<input type="password" id="email" placeholder="请输入密码">
    <script>
        var e=document.getElementById("email")
        e.onkeyup=function(event){
            if(event.keyCode==13){
                var value=this.value.trim();
              // 输入六位数密码
                var reg=/^\d{6}$/;
                console.log(reg.test(value))
            }
        }

    </script>

七、账户密码

<input type="password" id="email" placeholder="请输入密码">
    <script>
        var e=document.getElementById("email")
        e.onkeyup=function(event){
            if(event.keyCode==13){
                var value=this.value.trim();
              // 以字母开头,后面是数字,数字至少包含5位
                var reg=/^[a-zA-Z]+\d{5,}$/;
                console.log(reg.test(value))
            }
        }

    </script>

八、用户名验证

<input type="text" id="email" placeholder="请输入用户名">
    <script>
        var e=document.getElementById("email")
        e.onkeyup=function(event){
            if(event.keyCode==13){
                var value=this.value.trim();
                var reg=/^[a-zA-Z]{1,8}$/;
                console.log(reg.test(value))
            }
        }