10
0

3.元字符-边界符.html 885 B

123456789101112131415161718192021222324252627282930313233
  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. \b 单词边界
  11. const reg1 = /\ban\b/ig;
  12. const str1 = "1an Apple,An2 orange";
  13. console.log(str1.match(reg1));
  14. // 2.^ 匹配字母首行
  15. const reg2 = /^an/ig;
  16. const str2 = "an1 Apple, An2 orange an";
  17. console.log(str2.match(reg2));
  18. // 3.$ 匹配字母尾行
  19. const reg3 = /an$/ig;
  20. const str3 = "an1 Apple, An3 orange an";
  21. console.log(str3.match(reg3));
  22. const reg4 = /^a$/ig;
  23. // const str4 = "an1 Apple, An3 orange an";
  24. console.log(reg4.test('aaa'));
  25. console.log(reg4.test('a'));
  26. console.log(reg4.test(' '));
  27. </script>
  28. </body>
  29. </html>