5.元字符-字符类.html 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. // [] 任意匹配
  11. var reg = /[abc]/;
  12. console.log(reg.test("abc"));
  13. console.log(reg.test("acd"));
  14. console.log(reg.test("aiu"));
  15. console.log(reg.test("cud"));
  16. console.log(reg.test("bhd"));
  17. console.log(reg.test("uul"));
  18. // - 连字符
  19. const reg1 = /a-z/;//匹配小写26个字母
  20. const reg2 = /A-Z/;//匹配大写26个字母
  21. const reg3 = /0-9/;//匹配数字
  22. const reg4 = /a-zA-Z0-9_/;//匹配密码:英文+数字+_
  23. // ^ 取反 不在范围内
  24. const reg5 = /[^0-9]/;
  25. console.log(reg5.test('0909'));
  26. console.log(reg5.test('aaa'));
  27. // . 除换行符外的任意字符
  28. // 换行符 \n \r
  29. const reg6 = /./;
  30. console.log(reg6.test("aaa"));
  31. console.log(reg6.test(""));// 空字符串是false
  32. console.log(reg6.test("1124"));
  33. console.log(reg6.test("\n"));
  34. console.log(reg6.test("??"));
  35. console.log(reg6.test("\r"));
  36. </script>
  37. </body>
  38. </html>