Hướng dẫn replace all match string in javascript - thay thế tất cả chuỗi khớp trong javascript

Phương thức replaceAll() trả về một chuỗi mới với tất cả các trận đấu của pattern được thay thế bằng replacement. pattern có thể là một chuỗi hoặc ____10 và replacement có thể là một chuỗi hoặc một hàm được gọi cho mỗi trận đấu. Chuỗi ban đầu không thay đổi.replaceAll() method returns a new string with all matches of a pattern replaced by a replacement. The pattern can be a string or a

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, and the replacement can be a string or a function to be called for each match. The original string is left unchanged.

Thử nó

Cú pháp

replaceAll(pattern, replacement)

Thông số

pattern

Có thể là một chuỗi hoặc một đối tượng có phương thức

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."
3 - ví dụ điển hình là một biểu thức chính quy. Bất kỳ giá trị nào không có phương thức
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."
3 sẽ bị ép buộc thành một chuỗi.

Nếu pattern là một regex, thì nó phải có bộ cờ toàn cầu (

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."
6) hoặc
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."
7 bị ném.

replacement

Có thể là một chuỗi hoặc một hàm. Sự thay thế có cùng ngữ nghĩa như của

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.

Giá trị trả về

Một chuỗi mới, với tất cả các trận đấu của một mẫu được thay thế bằng một sự thay thế.

Ngoại lệ

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."
7

Ném nếu pattern là một regex không có bộ cờ toàn cầu (

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."
6) (thuộc tính
"xxx".replaceAll("", "_"); // "_x_x_x_"
3 của nó không chứa
"xxx".replaceAll("", "_"); // "_x_x_x_"
4).

Sự mô tả

Phương thức này không làm thay đổi giá trị chuỗi mà nó được gọi. Nó trả về một chuỗi mới.

Không giống như

"xxx".replaceAll("", "_"); // "_x_x_x_"
5, phương pháp này sẽ thay thế tất cả các lần xuất hiện của một chuỗi, không chỉ phương pháp đầu tiên. Điều này đặc biệt hữu ích nếu chuỗi không được biết đến một cách thống kê, vì gọi hàm xây dựng
"xxx".replaceAll("", "_"); // "_x_x_x_"
6 mà không thoát khỏi các ký tự đặc biệt có thể vô tình thay đổi ngữ nghĩa của nó.

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."

Nếu pattern là một đối tượng có phương thức

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."
3 (bao gồm các đối tượng
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), phương thức đó được gọi với chuỗi đích và replacement làm đối số. Giá trị trả về của nó trở thành giá trị trả về của replaceAll(). Trong trường hợp này, hành vi của replaceAll() hoàn toàn được mã hóa bằng phương pháp
"aabbcc".replaceAll("b", ".");
// 'aa..cc'
3 và do đó sẽ có kết quả tương tự như
"xxx".replaceAll("", "_"); // "_x_x_x_"
5 (ngoài xác thực đầu vào bổ sung rằng regex là toàn cầu).

Nếu pattern là một chuỗi trống, sự thay thế sẽ được chèn vào giữa mỗi đơn vị mã UTF-16, tương tự như hành vi

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

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

Để biết thêm thông tin về cách các thuộc tính Regex (đặc biệt là cờ dính) tương tác với replaceAll(), xem

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

Ví dụ

Sử dụng thay thế ()

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

REGEX không toàn cầu ném ném

Khi sử dụng giá trị tìm kiếm biểu thức thông thường, nó phải là toàn cầu. Điều này sẽ không hoạt động:

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

Điều này sẽ hoạt động:

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

Thông số kỹ thuật

Sự chỉ rõ
Đặc tả ngôn ngữ Ecmascript # sec-string.prototype.replaceall
# sec-string.prototype.replaceall

Tính tương thích của trình duyệt web

Bảng BCD chỉ tải trong trình duyệt

Xem thêm

Làm thế nào thay thế tất cả các lần xuất hiện của một chuỗi trong TypeScript?

Để thay thế tất cả các lần xuất hiện của một chuỗi trong TypeScript, hãy sử dụng phương thức thay thế (), chuyển nó một biểu thức thông thường bằng cờ G (Tìm kiếm toàn cầu).Ví dụ, str.Thay thế (/cũ/g, 'mới') trả về một chuỗi mới trong đó tất cả các lần xuất hiện của cũ được thay thế bằng mới.use the replace() method, passing it a regular expression with the g (global search) flag. For example, str. replace(/old/g, 'new') returns a new string where all occurrences of old are replaced with new .

Thay thế () trong javascript là gì?

Phương thức thay thế () tìm kiếm một chuỗi cho một giá trị hoặc biểu thức chính quy.Phương thức thay thế () trả về một chuỗi mới với (các) giá trị được thay thế.Phương thức thay thế () không thay đổi chuỗi gốc.searches a string for a value or a regular expression. The replace() method returns a new string with the value(s) replaced. The replace() method does not change the original string.

Khi nào được thêm vào JavaScript?

REPETEALL () đã được giới thiệu trong Chrome 85 (tháng 8 năm 2020) và Node.js 15. Tính đến năm 2022, chúng tôi không khuyên bạn nên sử dụng RepaceALL () do hỗ trợ hạn chế.August 2020) and Node. js 15. As of 2022, we do not recommend using replaceAll() due to limited support.