Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Cách thay thế chuỗi dùng JavaScript RegExp

Để thay thế chuỗi bằng RegExp trong JavaScript, sử dụng phương thức replace() của chuỗi.

Ví dụ

<html>
  <head>
    <script>
      var str = "Welcome to Tutorialspoint";
      document.write(str);
      var res = str.replace(/tutorialspoint/i, "Qries");
      document.write("<br>" + res);
    </script>
  </head>
  <body>
  </body>
</html>