4.字符类.html 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. // const reg = /[abc]/;
  12. // console.log(reg.test("ababab"))
  13. // console.log(reg.test("abc"))
  14. // console.log(reg.test("acacac"))
  15. // console.log(reg.test("acbbca"))
  16. // console.log(reg.test("dds"))
  17. // - 连字符
  18. // const reg1 = /0-9/;
  19. // const reg2 = /a-z/;
  20. // const reg3 = /A-Z/;
  21. // const reg4 = /a-zA-Z0-9_/;
  22. /**
  23. *
  24. * \w => /a-zA-Z0-9_/
  25. * \d => /0-9/
  26. * \W => /[^a-zA-Z0-9_]/
  27. * \D => /[^0-9]/
  28. */
  29. // 取反
  30. // const reg = /\D/;
  31. // console.log(reg.test('212'))
  32. // . 匹配换行外的任意字符
  33. const reg = /./;
  34. console.log(reg.test("aaa"))
  35. console.log(reg.test("1234567897654"))
  36. console.log(reg.test("")) // false
  37. console.log(reg.test("\n"))
  38. console.log(reg.test("\r"))
  39. console.log(reg.test("\f"))
  40. </script>
  41. </body>
  42. </html>