1. <script>
  2. /*
  3. 规定字符出行的次数
  4. 正则 test()
  5. */
  6. var reg = /[0-9]{6}/
  7. var pwd = 1234576;
  8. var t = reg.test(pwd);
  9. console.log(t)
  10. </script>
  1. <!--
  2. {m} 就出行m次
  3. {m,}至少出行m次
  4. {m,n}
  5. -->
  6. <script>
  7. var str = "2123jhfdd"
  8. var reg = /\d{6,}/;
  9. console.log(reg.test(str))
  10. </script>
  1. <!--
  2. {3,}
  3. {3}
  4. {3,6}
  5. ?{0,1}
  6. +{1,}
  7. *{0,}
  8. -->
  9. <script>
  10. var str = "hello123";
  11. var reg = /[a-z]+/;
  12. console.log(str.replace(reg,"*"))
  13. console.log(str.replace(/[a-z]*/,"*"))
  14. </script>

贪婪和懒惰

  1. <script>
  2. // 贪婪模式:在给定量词的情况下,会取最大的量词
  3. var str = "1231231hello";
  4. var reg = /\d{3,6}/;
  5. console.log(str.replace(reg,"*"))
  6. //test 只要正则表达式的某一段满足,就输出结果为true
  7. console.log(reg.test(str))
  8. // tips:+,*默认就是贪婪模式
  9. </script>

懒惰模式

  1. <script>
  2. //懒惰模式
  3. var str = "123123hello";
  4. var reg = /\d{3,6}?/;
  5. console.log(str.replace(reg,"*"))
  6. </script>

选择和分组

  1. <script>
  2. /*
  3. |
  4. ();
  5. */
  6. var str = "你去哪里,他在..."
  7. var reg = /你去|他在/g;
  8. console.log(str.replace(reg,"*"))
  9. </script>
  1. <script>
  2. var url = "http://www.baidu.com";
  3. var https = "https:www.daidu.com"
  4. var reg = /(http|https):\/\/www\baidu\.com/;
  5. console.log(reg.test(url))
  6. console.log(reg.test(https))
  7. </script>
  1. <script>
  2. var phone = "02788667777";
  3. //^ 后面一个$ 严格匹配的意思必须满足正则条件
  4. var reg = /^(027)?8\d{7}$/;
  5. console.log(reg.test(phone))
  6. </script>
  1. <input type="text" id="app">
  2. <script>
  3. //(086)?1[356789]\d{9}$
  4. var reg = /^(086)?1[3-9]\d{9}$/;
  5. $("#app").keyup(function(event){
  6. if(event.keyCode ==13){
  7. var value = $(this).val();
  8. console.log(reg.test(value))
  9. }
  10. })
  11. </script>