6.分组.html 1.0 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. // 分组
  11. const reg = /(ab)(cd)/;
  12. console.log(reg.test("abababacd"));
  13. console.log(reg.test("abab"));
  14. console.log(reg.test("abecd"));
  15. // 分组捕获
  16. const data = '2025-08-19';
  17. const reg1 = /^(\d{4})-(\d{2})-(\d{2})$/;
  18. console.log(reg1.test(data))
  19. console.log(data.replace(reg1,'$2/$3/$1'))
  20. // 分支结构
  21. const a1 = '西游记';
  22. const a2 = '水浒传';
  23. const a3 = '红楼梦';
  24. const a4 = '三国演义';
  25. const reg2 = /西游记|红楼梦/;
  26. console.log(reg2.test(a1));
  27. console.log(reg2.test(a2));
  28. console.log(reg2.test(a3));
  29. console.log(reg2.test(a4));
  30. /**
  31. * 1.安装插件any-rule
  32. * 2.https://regexper.com/
  33. * 可视化表达式
  34. */
  35. </script>
  36. </body>
  37. </html>