6.分组.html 952 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. // 分组
  11. const reg = /(ab)(cd)/;
  12. console.log(reg.test('abaabbbdc'));
  13. console.log(reg.test('abab'));
  14. // 分组捕获 $1 $2 $3
  15. // 2024-04-17
  16. const data = '2024-04-17'
  17. const reg1 = /^(\d{4})-(\d{2})-(\d{2})$/
  18. console.log(reg1.test(data))
  19. // 04/17/2024
  20. console.log(data.replace(reg1,'$2~$3~$1'))
  21. // 分支结构
  22. const a1 = '西游记';
  23. const a2 = '水浒传';
  24. const a3 = '红楼梦';
  25. const a4 = '三国演义';
  26. const reg2 = /西游记|红楼梦/;
  27. console.log(reg2.test(a1));
  28. console.log(reg2.test(a2));
  29. console.log(reg2.test(a3));
  30. console.log(reg2.test(a4));
  31. </script>
  32. </body>
  33. </html>