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

1234567891011121314151617181920212223242526272829303132333435363738394041
  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]/ //任意匹配abc
  12. // console.log(reg.test('abc'))
  13. // console.log(reg.test('abandon'))
  14. // console.log(reg.test('banana'))
  15. // console.log(reg.test('car'))
  16. // console.log(reg.test('listen'))
  17. // console.log(reg.test('tomorrow'))
  18. // 连字符
  19. // const reg1 = /[a-z]/;//a-z的26个字母任意匹配
  20. // const reg2 = /[A-Z]/; //A-Z的26个字母任意匹配
  21. // const reg3 = /[0-9]/; //0-9的数字任意匹配
  22. // const reg4 = /[a-zA-Z0-9_]/
  23. // ^ 取反
  24. const reg1 = /[^0-9]/
  25. console.log(reg1.test("1111"))
  26. console.log(reg1.test("abc"))
  27. // . 除换行外的任意字符
  28. const reg = /./
  29. console.log(reg.test(''))//. 匹配空字符串是false
  30. console.log(reg.test('abc'))
  31. console.log(reg.test('\n'))
  32. console.log(reg.test('\r'))
  33. console.log(reg.test('34567'))
  34. </script>
  35. </body>
  36. </html>