Hướng dẫn replaceall is not a function nodejs - replaceall không phải là một chức năng nodejs

Lý do tại sao điều này xảy ra?

Nếu bạn đang thấy lỗi "

let string = ":insertx: :insertx: :inserty: :inserty: :insertz: :insertz:";
let newstring = string.replaceAll(":insertx:", 'hello!');
0", có khả năng là do phương thức không được thực hiện/hỗ trợ bởi phiên bản trình duyệt (hoặc phiên bản Node.js) mà bạn đang sử dụng.

Lưu ý rằng phương pháp

let string = ":insertx: :insertx: :inserty: :inserty: :insertz: :insertz:";
let newstring = string.replaceAll(":insertx:", 'hello!');
1 đã được thêm vào trong ES2021/ES12.

Làm thế nào để khắc phục sự cố?

Để thay thế cho

let string = ":insertx: :insertx: :inserty: :inserty: :insertz: :insertz:";
let newstring = string.replaceAll(":insertx:", 'hello!');
1, bạn có thể sử dụng phương pháp
let string = ":insertx: :insertx: :inserty: :inserty: :insertz: :insertz:";
let newstring = string.replaceAll(":insertx:", 'hello!');
3 với một biểu thức thông thường có bộ cờ toàn cầu ("
let string = ":insertx: :insertx: :inserty: :inserty: :insertz: :insertz:";
let newstring = string.replaceAll(":insertx:", 'hello!');
4"). Nó hoạt động theo cùng một cách và có hỗ trợ trình duyệt tuyệt vời.

Ví dụ sau đây cho thấy

let string = ":insertx: :insertx: :inserty: :inserty: :insertz: :insertz:";
let newstring = string.replaceAll(":insertx:", 'hello!');
5 và nó tương đương bằng cách sử dụng
let string = ":insertx: :insertx: :inserty: :inserty: :insertz: :insertz:";
let newstring = string.replaceAll(":insertx:", 'hello!');
6:

const str = 'foo-bar';

// in older browsers
const result1 = str.replace(/foo/g, 'moo');

// ES12+
const result2 = str.replaceAll('foo', 'moo');

// output: 'moo-bar'
console.log(result1);
console.log(result2);

Để biết thêm các ví dụ, hãy xem bài đăng của chúng tôi về cách thay thế tất cả các lần xuất hiện của một từ trong chuỗi JavaScript.


Hy vọng bạn tìm thấy bài viết này hữu ích. Nó được xuất bản ngày 23 tháng 2 năm 2021. Vui lòng thể hiện tình yêu và sự hỗ trợ của bạn bằng cách chia sẻ bài đăng này.

Trang tài liệu: https://developer.mozilla.org/en-us/docs/web/javascript

let string = ":insertx: :insertx: :inserty: :inserty: :insertz: :insertz:";
let newstring = string.replaceAll(":insertx:", 'hello!');

Khi tôi chạy cái này, tôi nhận được

let string = ":insertx: :insertx: :inserty: :inserty: :insertz: :insertz:";
let newstring = string.replaceAll(":insertx:", 'hello!');
7. Có thể tôi đang hiểu sai nguyên mẫu là gì, nhưng hàm dường như là một phương thức chuỗi có sẵn để sử dụng.

Tôi đang sử dụng Chrome.

hỏi ngày 9 tháng 7 năm 2020 lúc 23:51Jul 9, 2020 at 23:51

pyknight202pyknight202pyknight202

1.1181 Huy hiệu vàng4 Huy hiệu bạc19 Huy hiệu đồng1 gold badge4 silver badges19 bronze badges

9

Sử dụng

let string = ":insertx: :insertx: :inserty: :inserty: :insertz: :insertz:";
let newstring = string.replaceAll(":insertx:", 'hello!');
8 với biểu thức thông thường với công cụ sửa đổi toàn cầu để hỗ trợ trình duyệt tốt hơn. (Kiểm tra bảng tương thích trình duyệt trên MDN để xem phiên bản nào của mỗi trình duyệt bắt đầu hỗ trợ phương thức
let string = ":insertx: :insertx: :inserty: :inserty: :insertz: :insertz:";
let newstring = string.replaceAll(":insertx:", 'hello!');
9.)

let string = ":insertx: :insertx: :inserty: :inserty: :insertz: :insertz:";
let newstring = string.replace(/:insertx:/g, 'hello!');
console.log(newstring);

Đối với một giải pháp chung chung hơn, chúng ta có thể thoát khỏi các metacharacters biểu thức chính quy và sử dụng hàm tạo

let string = ":insertx: :insertx: :inserty: :inserty: :insertz: :insertz:";
let newstring = string.replace(/:insertx:/g, 'hello!');
console.log(newstring);
0. Bạn cũng có thể thêm hàm vào
let string = ":insertx: :insertx: :inserty: :inserty: :insertz: :insertz:";
let newstring = string.replace(/:insertx:/g, 'hello!');
console.log(newstring);
1 dưới dạng polyfill.

.

//Taken from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
function escapeRegExp(string) {
  return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
function replaceAll(str, match, replacement){
   return str.replace(new RegExp(escapeRegExp(match), 'g'), ()=>replacement);
}

console.log(replaceAll('a.b.c.d.e', '.', '__'));
console.log(replaceAll('a.b.c.d.e', '.', '$&'));

Một shim tuân thủ đặc điểm kỹ thuật có thể được tìm thấy ở đây.

Đã trả lời ngày 9 tháng 7 năm 2020 lúc 23:53Jul 9, 2020 at 23:53

Hướng dẫn replaceall is not a function nodejs - replaceall không phải là một chức năng nodejs

UnitigatedUnmitigatedUnmitigated

50,4K7 Huy hiệu vàng50 Huy hiệu bạc68 Huy hiệu Đồng7 gold badges50 silver badges68 bronze badges

let string = ":insertx: :insertx: :inserty: :inserty: :insertz: :insertz:";
let newstring = string.replace(/:insertx:/g, 'hello!');
console.log(newstring);
3 sẽ có sẵn bắt đầu trên Chrome 85. Phiên bản hiện tại là 83.

Nếu bạn tải xuống Google Chrome Canary (trên phiên bản 86), bạn sẽ có thể thấy mã của bạn chạy tốt. Firefox nằm trên phiên bản 78 và vì

let string = ":insertx: :insertx: :inserty: :inserty: :insertz: :insertz:";
let newstring = string.replace(/:insertx:/g, 'hello!');
console.log(newstring);
3 đã có sẵn phiên bản bắt đầu 77, nó cũng hoạt động ở đó. Nó cũng sẽ hoạt động trên Safari hiện tại. Microsoft Edge có nó không được hỗ trợ.

Bạn sẽ tìm thấy các phiên bản trình duyệt được hỗ trợ ở cuối bài viết trong câu hỏi của bạn.

Đã trả lời ngày 9 tháng 7 năm 2020 lúc 23:56Jul 9, 2020 at 23:56

Hướng dẫn replaceall is not a function nodejs - replaceall không phải là một chức năng nodejs

MattdiamantmattdiamantMattDiamant

8.3004 huy hiệu vàng35 Huy hiệu bạc46 Huy hiệu đồng4 gold badges35 silver badges46 bronze badges

Nếu bạn không muốn nâng cấp chrome của mình cũng như sử dụng các biểu thức reg (vì chúng ít hoạt động hơn), bạn cũng có thể làm điều này:

let string = ":insertx: :insertx: :inserty: :inserty: :insertz: :insertz:";
let newstring = string.split(":insertx:").join('hello!');

Và tất nhiên, bạn có thể gắn vào nguyên mẫu chuỗi nếu bạn thích nó ở mọi nơi. Nhưng vì Real RepaceALL thực tế được lấp đầy nhiều tính năng hơn (hỗ trợ Regex), bạn sẽ an toàn hơn khi làm:

String.prototype.replaceAllTxt = function replaceAll(search, replace) { return this.split(search).join(replace); }

Đã trả lời ngày 18 tháng 9 năm 2020 lúc 15:13Sep 18, 2020 at 15:13

Hướng dẫn replaceall is not a function nodejs - replaceall không phải là một chức năng nodejs

1

Bạn có thể tự xác định nó một cách dễ dàng:

if(typeof String.prototype.replaceAll === "undefined") {
    String.prototype.replaceAll = function(match, replace) {
       return this.replace(new RegExp(match, 'g'), () => replace);
    }
}

Và sử dụng nó:

"fafa".replaceAll("a", "o");
>>> fofo

Đã trả lời ngày 29 tháng 5 năm 2021 lúc 11:19May 29, 2021 at 11:19

Bọt Amir choAmir Fo

4.5231 Huy hiệu vàng39 Huy hiệu bạc48 Huy hiệu đồng1 gold badge39 silver badges48 bronze badges

2

Hàm

let string = ":insertx: :insertx: :inserty: :inserty: :insertz: :insertz:";
let newstring = string.replace(/:insertx:/g, 'hello!');
console.log(newstring);
5 được thêm vào trong ES2021 ES12, đó là lý do tại sao nó không được xác định trong các phiên bản cũ hơn của trình duyệt và NodeJS.

Hướng dẫn replaceall is not a function nodejs - replaceall không phải là một chức năng nodejs

Eric Aya

69.1K35 Huy hiệu vàng178 Huy hiệu bạc249 Huy hiệu đồng35 gold badges178 silver badges249 bronze badges

Đã trả lời ngày 20 tháng 11 năm 2021 lúc 13:20Nov 20, 2021 at 13:20

Hướng dẫn replaceall is not a function nodejs - replaceall không phải là một chức năng nodejs

Mặc dù hơi lạc lõng, nhưng tôi tình cờ phát hiện ra trường hợp sử dụng của tôi không được liệt kê, vì vậy đây là dành cho một người như tôi. Tôi cần phải ẩn một từ nhưng nếu nó bắt đầu với một số ký tự nhất định, tức là Dev. Và, có nhân vật thẻ hoang dã * đã giúp tôi làm điều đó.starts with certain characters i.e. dev. And, there is wild-card character * that helped me do it.

'Fullstack developer'.replace(/dev.*/g, '') // => Fullstack

Lưu ý: Lưu ý dấu chấm.

Đã trả lời ngày 29 tháng 12 năm 2020 lúc 14:17Dec 29, 2020 at 14:17

Hướng dẫn replaceall is not a function nodejs - replaceall không phải là một chức năng nodejs

LahorilahoriLahori

1.05110 Huy hiệu bạc20 Huy hiệu Đồng10 silver badges20 bronze badges

0

Tôi cũng đã nhận được các vấn đề lỗi loại tương tự với thay thế tất cả. Tôi đã giải quyết nó bằng phương thức thay thế với biểu thức thông thường với bộ cờ toàn cầu.

let unformattedDate = "06=07=2022";
const formattedString = unformattedDate.replace(/=/g, ':');
console.log(formattedString);

Đã trả lời ngày 7 tháng 6 lúc 6:46Jun 7 at 6:46

Hướng dẫn replaceall is not a function nodejs - replaceall không phải là một chức năng nodejs