1.认识正则表达式.html 816 B

12345678910111213141516171819202122232425262728
  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. const reg = /西游记/g;
  11. // 1.test()
  12. const result = reg.test("这是一本西游ddd记");
  13. console.log(result);
  14. // 2.exec()
  15. const result1 = reg.exec("这是一本西游记,西游记");
  16. console.log(result1);
  17. // 3.replace()
  18. var str = '一段西游记的文字';
  19. const result2 = str.replace(reg,'红楼梦');
  20. console.log(result2);
  21. // 4.match()
  22. var str1 = '一本书西游记,我也爱看西游记,西游记呀';
  23. const result3 = str1.match(reg);
  24. console.log(result3);
  25. </script>
  26. </body>
  27. </html>