7.案例.html 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. // 1.手机号脱敏 13789430987 => 137****0987
  11. const str1 = '13789430987';
  12. const reg1 = /^(1[0-9]{2})[0-9]{4}([0-9]{4})$/
  13. console.log(reg1.test(str1),'1')
  14. console.log(str1.replace(reg1,'$1****$2'))
  15. // 2.密码匹配 (6-16字母、数字、下划线)
  16. const str2 = 'hello123456_';
  17. // const reg2 = /^[a-zA-Z0-9_]{6,16}$/
  18. const reg2 = /^\w{6,16}$/;
  19. // const reg2 = /^[a-zA-Z0-9_]{6,16}$/;
  20. console.log(reg2.test(str2),'2')
  21. // 3.匹配16进制颜色 #ff0000 #0f0
  22. const str3 = '#ff0000';
  23. const str4 = '#f0f';
  24. const reg3 = /^([a-fA-F0-9]{6})|([a-fA-F0-9]{3})$/
  25. // const reg3 = /^#([a-fA-F0-9]{6})|([a-fA-F0-9]{3})$/;
  26. console.log(reg3.test(str3),'3');
  27. console.log(reg3.test(str4),'4');
  28. // 4.匹配24小时时间 23:59 20:12 08:35 18:22
  29. //0 0-9
  30. //1 0-9
  31. // 2 0-3 23:59=>00:00
  32. // 5 0-5
  33. const reg4 = /^([0-1][0-9])|([2][0-3]):[0-5][0-9]$/
  34. // const reg4 = /^([0-1][0-9])|([2][0-3]):[0-5][0-9]$/
  35. console.log(reg4.test("12:36"));
  36. console.log(reg4.test("20:12"));
  37. console.log(reg4.test("27:40"));
  38. </script>
  39. </body>
  40. </html>