1. <script>
  2. //42........x
  3. //42 112620001126171 x
  4. //42\d{15}(\d|x)
  5. var str = "42110619881024307x";
  6. var res = /^42\d{15}[\dx]$/;
  7. console.log(res.test(str))
  8. </script>
  1. <input type="text" placeholder="请输入密码" id="app">
  2. <script>
  3. /*
  4. 密码
  5. 必须包含大写小写至少6位数字且以大写字母开头
  6. */
  7. var reg = /^[A-Z]+[a-z]+\d{6,}$/
  8. $("#app").keyup(function(event){
  9. if(event.keyCode ==13){
  10. var value = $(this).val();
  11. console.log(reg.test(value))
  12. }
  13. })
  14. </script>

指定匹配位置

  1. <script>
  2. /*
  3. */
  4. var str = "1fiddewew"
  5. var reg = /^[0-9]+/
  6. console.log(reg.test(str))
  7. console.log(/\d+$/.test(str))
  8. var str = " hello ";
  9. var reg = /(^\s+)|(\s+$)/g
  10. console.log(str.trim())
  11. console.log()
  12. </script>

  1. <script>
  2. var str = "abc123";
  3. var reg = /[^abc]/g;
  4. console.log(str.replace(reg,"*"))
  5. var test = "?@hello123-";
  6. /*123*/
  7. var reg = /[^\d]/g;
  8. console.log(test.replace(reg,"*"))
  9. </script>