1. 正则表达式:规定字符串出行规定的表达式
  2. 用户登录注册的时候的密码用户名的效验
  3. 关键字的过滤 429xxxxxxxxxxxxxxx3037
  1. var str ="你好你的你们"
  2. var reg = /你/g;
  3. //g修饰符--全部过滤
  4. console.log(str.replace(reg,"*"))

方法2

  1. <script>
  2. var reg = new RegExp(/你/,"g"); //声明一个过滤的规则
  3. var str = "你好你的你们"
  4. console.log(str.replace(reg,"*"))
  5. </script>

被选字符集

  1. <script>
  2. /*
  3. 被选字符集
  4. */
  5. var str = "it12e34m56342442";
  6. /*str="item"*/
  7. var reg = /[0-9]/g;
  8. console.log(str.replace(reg,""))
  9. </script>

备选字符集

  1. <script>
  2. // 备选字符--规定某一个字符的备选字符列表
  3. // 特点:多个里面选一个
  4. var str = "上海,上天,上哪";
  5. var reg = /上[海天哪]/g;
  6. console.log(str.replace(reg,"*"))
  7. var s= "你sb,他sx,她sd";
  8. var r = /s[bxd]/g;
  9. console.log(s.replace(r,"*"))
  10. </script>

预定字符集

  1. <!-- 预定字符集
  2. [0-9]
  3. g选择全部的字符
  4. i忽略大小写
  5. -->
  6. <script>
  7. var str = "dfsdjl9873j388TY";
  8. var reg = /[0-9]/g;
  9. console.log(str.replace(reg,""))
  10. console.log(str.replace(/[a-z]/ig,"*"))
  11. </script>
  1. <script>
  2. var str = "http://www.daidu.com/123456"
  3. var res = /[0-9]/g;
  4. //console.log(str.replace(res,""))
  5. var arr =[
  6. {id:1001,name:"你好345ffd"},
  7. {id:1002,name:"你好杀害的的哈哈34232155ffd"},
  8. {id:1003,name:"附加费3445ffd"},
  9. ]
  10. var res =/[0-9a-z]/g;
  11. arr.forEach(item=>{
  12. var name = item.name.replace(res,"")+"...";
  13. console.log(name)
  14. item.name = name;
  15. })
  16. </script>