Hướng dẫn javascript replace all - javascript thay thế tất cả

Hi 🤓 Cảm ơn bạn đã ghé thăm blog này, nếu những bài viết trên blog giúp ích cho bạn. Bạn có thể giúp blog hiển thị quảng cáo bằng cách tạm ngừng ad blocker 😫 và để giúp blog duy trì hoạt động nếu bạn muốn. Cảm ơn bạn!ad blocker 😫 và để giúp blog duy trì hoạt động nếu bạn muốn.
Cảm ơn bạn!

Trong bài viết này, chúng ta cùng tìm hiểu về method replaceAll trong Javascript để xem method này có chức năng gì nhé!

replaceAll() method

Để hiểu hơn về 'homiedevhomiedev'.replace(/homiedev/g, 'blog'); // blogblog0 chúng ta cùng tìm hiểu về method 'homiedevhomiedev'.replace(/homiedev/g, 'blog'); // blogblog1 trước nhé.

Method 'homiedevhomiedev'.replace(/homiedev/g, 'blog'); // blogblog1 giúp chúng ta thay thế một substring nằm trong chuỗi muốn thay thế.

Ta có ví dụ như sau:

'homiedevhomiedev'.replace('homiedev', 'blog'); // bloghomiedev'

Ở ví dụ trên, ta thực hiện thay thế substring là 'homiedevhomiedev'.replace(/homiedev/g, 'blog'); // blogblog3 thành 'homiedevhomiedev'.replace(/homiedev/g, 'blog'); // blogblog4. Tuy nhiên khi sử dụng 'homiedevhomiedev'.replace(/homiedev/g, 'blog'); // blogblog1 ta chỉ có thể thay thế được chuỗi 'homiedevhomiedev'.replace(/homiedev/g, 'blog'); // blogblog3 ở lần xuất hiện đầu tiên và các lần xuất hiện tiếp theo sẽ bị bỏ qua.

Trong trường hợp bạn muốn thay thế tất cả các chuỗi, chúng ta có thể sử dụng 'homiedevhomiedev'.replace(/homiedev/g, 'blog'); // blogblog7 như sau:

'homiedevhomiedev'.replace(/homiedev/g, 'blog'); // blogblog

Sử dụng 'homiedevhomiedev'.replace(/homiedev/g, 'blog'); // blogblog7 giúp chúng ta có thể get hoặc replace các substring trong các trường hợp phức tạp. Còn trong trường hợp trên ta có thể sử dụng một cách đơn giản hơn đó là dùng method 'homiedevhomiedev'.replace(/homiedev/g, 'blog'); // blogblog0.

Chúng ta đơn giản chỉ cần như sau:

'homiedevhomiedev'.replaceAll('homiedev', 'blog'); // blogblog

Như bạn thấy ở trên, kết quả ta nhận được giống như khi sử dụng 'homiedevhomiedev'.replace(/homiedev/g, 'blog'); // blogblog7, bạn có thể sử dụng một trong những cách này để thay thể chuỗi theo ý muốn ^^.

Kết luận

Như vậy là chúng ta đã tìm hiểu xong method 'homiedevhomiedev'.replace(/homiedev/g, 'blog'); // blogblog0, hy vọng bài viết giúp ích cho các bạn.

Chúng ta sẽ gặp lại sau trong các bài viết sắp tới nhé!

Một số bài viết JavaScript nên đọc:

  1. Callback Function trong JavaScript là gì? tại sao lại được sử dụng nhiều như vậy?
  2. Tìm hiểu về Memoization trong JavaScript
  3. Tìm hiểu về method startsWith JavaScript thông qua ví dụ đơn giản
  4. some js là gì? Tìm hiểu về method some trong JavaScript

About Me

Hi, I'm @devnav. Một người thích chia sẻ kiến thức, đặc biệt là về Frontend 🚀. Trang web này được tạo ra nhằm giúp các bạn học Frontend hiệu quả hơn 🎉😄.@devnav. Một người thích chia sẻ kiến thức, đặc biệt là về Frontend 🚀. Trang web này được tạo ra nhằm giúp các bạn học Frontend hiệu quả hơn 🎉😄.

Chúc các bạn tìm được kiến thức hữu ích trong blog này 😁😁.

The 'homiedevhomiedev'.replace(/homiedev/g, 'blog'); // blogblog0 method returns a new string with all matches of a 'homiedevhomiedev'.replaceAll('homiedev', 'blog'); // blogblog3 replaced by a 'homiedevhomiedev'.replaceAll('homiedev', 'blog'); // blogblog4. The 'homiedevhomiedev'.replaceAll('homiedev', 'blog'); // blogblog3 can be a string or a 'homiedevhomiedev'.replaceAll('homiedev', 'blog'); // blogblog6, and the 'homiedevhomiedev'.replaceAll('homiedev', 'blog'); // blogblog4 can be a string or a function to be called for each match. The original string is left unchanged.'homiedevhomiedev'.replace(/homiedev/g, 'blog'); // blogblog0 method returns a new string with all matches of a 'homiedevhomiedev'.replaceAll('homiedev', 'blog'); // blogblog3 replaced by a 'homiedevhomiedev'.replaceAll('homiedev', 'blog'); // blogblog4. The 'homiedevhomiedev'.replaceAll('homiedev', 'blog'); // blogblog3 can be a string or a 'homiedevhomiedev'.replaceAll('homiedev', 'blog'); // blogblog6, and the 'homiedevhomiedev'.replaceAll('homiedev', 'blog'); // blogblog4 can be a string or a function to be called for each match. The original string is left unchanged.

Try it

Syntax

replaceAll(pattern, replacement)

Parameters

'homiedevhomiedev'.replaceAll('homiedev', 'blog'); // blogblog3

Can be a string or an object with a 'homiedevhomiedev'.replaceAll('homiedev', 'blog'); // blogblog9 method — the typical example being a regular expression. Any value that doesn't have the 'homiedevhomiedev'.replaceAll('homiedev', 'blog'); // blogblog9 method will be coerced to a string.

If 'homiedevhomiedev'.replaceAll('homiedev', 'blog'); // blogblog3 is a regex, then it must have the global (replaceAll(pattern, replacement) 2) flag set, or a replaceAll(pattern, replacement) 3 is thrown.

'homiedevhomiedev'.replaceAll('homiedev', 'blog'); // blogblog4

Can be a string or a function. The replacement has the same semantics as that of replaceAll(pattern, replacement) 5.

Return value

A new string, with all matches of a pattern replaced by a replacement.

Exceptions

replaceAll(pattern, replacement) 3

Thrown if the 'homiedevhomiedev'.replaceAll('homiedev', 'blog'); // blogblog3 is a regex that does not have the global (replaceAll(pattern, replacement) 2) flag set (its replaceAll(pattern, replacement) 9 property does not contain function unsafeRedactName(text, name) { return text.replace(new RegExp(name, 'g'), '[REDACTED]'); } function safeRedactName(text, name) { return text.replaceAll(name, '[REDACTED]'); } const report = "A hacker called ha.*er used special characters in their name to breach the system."; console.log(unsafeRedactName(report, "ha.*er")); // "A [REDACTED]s in their name to breach the system." console.log(safeRedactName(report, "ha.*er")); // "A hacker called [REDACTED] used special characters in their name to breach the system." 0).

Description

This method does not mutate the string value it's called on. It returns a new string.

Unlike 'homiedevhomiedev'.replace(/homiedev/g, 'blog'); // blogblog1, this method would replace all occurrences of a string, not just the first one. This is especially useful if the string is not statically known, as calling the function unsafeRedactName(text, name) { return text.replace(new RegExp(name, 'g'), '[REDACTED]'); } function safeRedactName(text, name) { return text.replaceAll(name, '[REDACTED]'); } const report = "A hacker called ha.*er used special characters in their name to breach the system."; console.log(unsafeRedactName(report, "ha.*er")); // "A [REDACTED]s in their name to breach the system." console.log(safeRedactName(report, "ha.*er")); // "A hacker called [REDACTED] used special characters in their name to breach the system." 2 constructor without escaping special characters may unintentionally change its semantics.

function unsafeRedactName(text, name) { return text.replace(new RegExp(name, 'g'), '[REDACTED]'); } function safeRedactName(text, name) { return text.replaceAll(name, '[REDACTED]'); } const report = "A hacker called ha.*er used special characters in their name to breach the system."; console.log(unsafeRedactName(report, "ha.*er")); // "A [REDACTED]s in their name to breach the system." console.log(safeRedactName(report, "ha.*er")); // "A hacker called [REDACTED] used special characters in their name to breach the system."

If 'homiedevhomiedev'.replaceAll('homiedev', 'blog'); // blogblog3 is an object with a 'homiedevhomiedev'.replaceAll('homiedev', 'blog'); // blogblog9 method (including 'homiedevhomiedev'.replaceAll('homiedev', 'blog'); // blogblog6 objects), that method is called with the target string and 'homiedevhomiedev'.replaceAll('homiedev', 'blog'); // blogblog4 as arguments. Its return value becomes the return value of 'homiedevhomiedev'.replace(/homiedev/g, 'blog'); // blogblog0. In this case the behavior of 'homiedevhomiedev'.replace(/homiedev/g, 'blog'); // blogblog0 is entirely encoded by the function unsafeRedactName(text, name) { return text.replace(new RegExp(name, 'g'), '[REDACTED]'); } function safeRedactName(text, name) { return text.replaceAll(name, '[REDACTED]'); } const report = "A hacker called ha.*er used special characters in their name to breach the system."; console.log(unsafeRedactName(report, "ha.*er")); // "A [REDACTED]s in their name to breach the system." console.log(safeRedactName(report, "ha.*er")); // "A hacker called [REDACTED] used special characters in their name to breach the system." 9 method, and therefore will have the same result as 'homiedevhomiedev'.replace(/homiedev/g, 'blog'); // blogblog1 (apart from the extra input validation that the regex is global).

If the 'homiedevhomiedev'.replaceAll('homiedev', 'blog'); // blogblog3 is an empty string, the replacement will be inserted in between every UTF-16 code unit, similar to "xxx".replaceAll("", "_"); // "_x_x_x_" 2 behavior.

"xxx".replaceAll("", "_"); // "_x_x_x_"

For more information about how regex properties (especially the sticky flag) interact with 'homiedevhomiedev'.replace(/homiedev/g, 'blog'); // blogblog0, see "xxx".replaceAll("", "_"); // "_x_x_x_" 4.

Examples

Using replaceAll()

'aabbcc'.replaceAll('b', '.'); // 'aa..cc'

Non-global regex throws

When using a regular expression search value, it must be global. This won't work:

'aabbcc'.replaceAll(/b/, '.'); // TypeError: replaceAll must be called with a global RegExp

This will work:

'aabbcc'.replaceAll(/b/g, '.'); "aa..cc"

Specifications

Specification
ECMAScript Language Specification # sec-string.prototype.replaceall
# sec-string.prototype.replaceall

Browser compatibility

BCD tables only load in the browser

See also

Chủ đề