Không có cách nào dễ dàng để thay thế tất cả các lần xuất hiện chuỗi trong JavaScript. Java, đã phục vụ một nguồn cảm hứng cho JavaScript trong những ngày đầu tiên, có phương pháp replaceAll() trên các chuỗi kể từ năm 1995! Trong bài đăng này, bạn sẽ học cách thay thế tất cả các lần xuất hiện chuỗi trong JavaScript bằng cách chia và nối một chuỗi và javascript
const resultingString = pieces.join(replace);
0 kết hợp với biểu thức chính quy toàn cầu.Hơn nữa, bạn sẽ đọc về chuỗi đề xuất mới.replaceall () (ở giai đoạn 4) mang lại tất cả các phương thức cho các chuỗi JavaScript. Đây là cách tiếp cận thuận tiện nhất. 1. Tách và tham gia một mảngNếu bạn Google làm thế nào để "thay thế tất cả các lần xuất hiện chuỗi trong JavaScript", rất có thể phương pháp đầu tiên bạn tìm thấy là sử dụng một mảng trung gian. Đây là cách nó hoạt động: - Chia
javascript
const resultingString = pieces.join(replace);
1 thành javascript
const resultingString = pieces.join(replace);
2 cho chuỗi javascript
const resultingString = pieces.join(replace);
3:
javascript
const pieces = string.split(search);
- Sau đó tham gia các phần đặt chuỗi
javascript
const resultingString = pieces.join(replace);
4 ở giữa:
javascript
const resultingString = pieces.join(replace);
Ví dụ: chúng ta hãy thay thế tất cả các khoảng trống javascript
const resultingString = pieces.join(replace);
5 bằng dấu gạch nối javascript
const resultingString = pieces.join(replace);
6 trong chuỗi javascript
const resultingString = pieces.join(replace);
7:javascript
const search = ' ';
const replaceWith = '-';
const result = 'duck duck go'.split(search).join(replaceWith);
result; // => 'duck-duck-go'
javascript
const resultingString = pieces.join(replace);
8 chia chuỗi thành các mảnh: javascript
const resultingString = pieces.join(replace);
9.Sau đó, các mảnh javascript
const search = ' ';
const replaceWith = '-';
const result = 'duck duck go'.split(search).join(replaceWith);
result; // => 'duck-duck-go'
0 được nối bằng cách chèn javascript
const resultingString = pieces.join(replace);
6 vào giữa chúng, dẫn đến chuỗi javascript
const search = ' ';
const replaceWith = '-';
const result = 'duck duck go'.split(search).join(replaceWith);
result; // => 'duck-duck-go'
2.Đây là một chức năng trợ giúp tổng quát sử dụng phương pháp phân tách và tham gia: javascript
function replaceAll(string, search, replace) {
return string.split(search).join(replace);
}
replaceAll('abba', 'a', 'i'); // => 'ibbi'
replaceAll('go go go!', 'go', 'move'); // => 'move move move!'
replaceAll('oops', 'z', 'y'); // => 'oops'
Cách tiếp cận này yêu cầu chuyển đổi chuỗi thành một mảng, và sau đó quay lại thành một chuỗi. Hãy tiếp tục tìm kiếm các lựa chọn thay thế tốt hơn. 2. Thay thế () bằng biểu thức chính quy toàn cầuPhương thức chuỗi javascript
const search = ' ';
const replaceWith = '-';
const result = 'duck duck go'.split(search).join(replaceWith);
result; // => 'duck-duck-go'
3 tìm kiếm và thay thế các lần xuất hiện của biểu thức chính quy javascript
const search = ' ';
const replaceWith = '-';
const result = 'duck duck go'.split(search).join(replaceWith);
result; // => 'duck-duck-go'
4 bằng chuỗi javascript
const search = ' ';
const replaceWith = '-';
const result = 'duck duck go'.split(search).join(replaceWith);
result; // => 'duck-duck-go'
5.Để thực hiện phương thức javascript
const search = ' ';
const replaceWith = '-';
const result = 'duck duck go'.split(search).join(replaceWith);
result; // => 'duck-duck-go'
6 thay thế tất cả các lần xuất hiện của mẫu, bạn phải kích hoạt cờ toàn cầu trên biểu thức thông thường:- Nối
javascript
const search = ' ';
const replaceWith = '-';
const result = 'duck duck go'.split(search).join(replaceWith);
result; // => 'duck-duck-go'
7 Sau khi kết thúc biểu thức thông thường theo nghĩa đen: javascript
const search = ' ';
const replaceWith = '-';
const result = 'duck duck go'.split(search).join(replaceWith);
result; // => 'duck-duck-go'
8 - Hoặc khi sử dụng hàm tạo biểu thức chính quy, hãy thêm
javascript
const search = ' ';
const replaceWith = '-';
const result = 'duck duck go'.split(search).join(replaceWith);
result; // => 'duck-duck-go'
9 vào đối số thứ hai: javascript
function replaceAll(string, search, replace) {
return string.split(search).join(replace);
}
replaceAll('abba', 'a', 'i'); // => 'ibbi'
replaceAll('go go go!', 'go', 'move'); // => 'move move move!'
replaceAll('oops', 'z', 'y'); // => 'oops'
0
Hãy thay thế tất cả các lần xuất hiện của javascript
const resultingString = pieces.join(replace);
5 bằng javascript
const resultingString = pieces.join(replace);
6:javascript
const searchRegExp = /\s/g;
const replaceWith = '-';
const result = 'duck duck go'.replace(searchRegExp, replaceWith);
result; // => 'duck-duck-go'
Biểu thức thông thường theo nghĩa đen javascript
function replaceAll(string, search, replace) {
return string.split(search).join(replace);
}
replaceAll('abba', 'a', 'i'); // => 'ibbi'
replaceAll('go go go!', 'go', 'move'); // => 'move move move!'
replaceAll('oops', 'z', 'y'); // => 'oops'
3 (lưu ý cờ toàn cầu javascript
const search = ' ';
const replaceWith = '-';
const result = 'duck duck go'.split(search).join(replaceWith);
result; // => 'duck-duck-go'
7) khớp với không gian javascript
const resultingString = pieces.join(replace);
5.javascript
function replaceAll(string, search, replace) {
return string.split(search).join(replace);
}
replaceAll('abba', 'a', 'i'); // => 'ibbi'
replaceAll('go go go!', 'go', 'move'); // => 'move move move!'
replaceAll('oops', 'z', 'y'); // => 'oops'
6 thay thế tất cả các trận đấu của javascript
function replaceAll(string, search, replace) {
return string.split(search).join(replace);
}
replaceAll('abba', 'a', 'i'); // => 'ibbi'
replaceAll('go go go!', 'go', 'move'); // => 'move move move!'
replaceAll('oops', 'z', 'y'); // => 'oops'
3 bằng javascript
const resultingString = pieces.join(replace);
6, dẫn đến javascript
const search = ' ';
const replaceWith = '-';
const result = 'duck duck go'.split(search).join(replaceWith);
result; // => 'duck-duck-go'
2.Bạn có thể dễ dàng thay thế trường hợp không nhạy cảm bằng cách thêm cờ javascript
const searchRegExp = /\s/g;
const replaceWith = '-';
const result = 'duck duck go'.replace(searchRegExp, replaceWith);
result; // => 'duck-duck-go'
0 vào biểu thức thông thường:javascript
const searchRegExp = /duck/gi;
const replaceWith = 'goose';
const result = 'DUCK Duck go'.replace(searchRegExp, replaceWith);
result; // => 'goose goose go'
Biểu thức thông thường javascript
const searchRegExp = /\s/g;
const replaceWith = '-';
const result = 'duck duck go'.replace(searchRegExp, replaceWith);
result; // => 'duck-duck-go'
1 thực hiện tìm kiếm không nhạy cảm với trường hợp toàn cầu (lưu ý javascript
const searchRegExp = /\s/g;
const replaceWith = '-';
const result = 'duck duck go'.replace(searchRegExp, replaceWith);
result; // => 'duck-duck-go'
0 và javascript
const search = ' ';
const replaceWith = '-';
const result = 'duck duck go'.split(search).join(replaceWith);
result; // => 'duck-duck-go'
7 cờ). javascript
const searchRegExp = /\s/g;
const replaceWith = '-';
const result = 'duck duck go'.replace(searchRegExp, replaceWith);
result; // => 'duck-duck-go'
1 khớp với javascript
const searchRegExp = /\s/g;
const replaceWith = '-';
const result = 'duck duck go'.replace(searchRegExp, replaceWith);
result; // => 'duck-duck-go'
5, cũng như javascript
const searchRegExp = /\s/g;
const replaceWith = '-';
const result = 'duck duck go'.replace(searchRegExp, replaceWith);
result; // => 'duck-duck-go'
6.Gọi javascript
const searchRegExp = /\s/g;
const replaceWith = '-';
const result = 'duck duck go'.replace(searchRegExp, replaceWith);
result; // => 'duck-duck-go'
7 thay thế tất cả các trận đấu của các chuỗi con javascript
const searchRegExp = /\s/g;
const replaceWith = '-';
const result = 'duck duck go'.replace(searchRegExp, replaceWith);
result; // => 'duck-duck-go'
1 với javascript
const searchRegExp = /\s/g;
const replaceWith = '-';
const result = 'duck duck go'.replace(searchRegExp, replaceWith);
result; // => 'duck-duck-go'
9.2.1 Biểu thức chính quy từ chuỗiKhi biểu thức chính quy được tạo từ một chuỗi, bạn phải thoát khỏi các ký tự javascript
const searchRegExp = /duck/gi;
const replaceWith = 'goose';
const result = 'DUCK Duck go'.replace(searchRegExp, replaceWith);
result; // => 'goose goose go'
0 vì chúng có ý nghĩa đặc biệt trong biểu thức thông thường.Do đó, các ký tự đặc biệt là một vấn đề khi bạn muốn thay thế tất cả các hoạt động. Đây là một ví dụ: javascript
const search = '+';
const searchRegExp = new RegExp(search, 'g'); // Throws SyntaxError
const replaceWith = '-';
const result = '5+2+1'.replace(searchRegExp, replaceWith);
Đoạn trích ở trên cố gắng chuyển đổi chuỗi tìm kiếm javascript
const searchRegExp = /duck/gi;
const replaceWith = 'goose';
const result = 'DUCK Duck go'.replace(searchRegExp, replaceWith);
result; // => 'goose goose go'
1 thành một biểu thức thông thường. Nhưng javascript
const searchRegExp = /duck/gi;
const replaceWith = 'goose';
const result = 'DUCK Duck go'.replace(searchRegExp, replaceWith);
result; // => 'goose goose go'
1 là một biểu thức chính quy không hợp lệ, do đó javascript
const searchRegExp = /duck/gi;
const replaceWith = 'goose';
const result = 'DUCK Duck go'.replace(searchRegExp, replaceWith);
result; // => 'goose goose go'
3 bị ném.Thoát khỏi nhân vật javascript
const searchRegExp = /duck/gi;
const replaceWith = 'goose';
const result = 'DUCK Duck go'.replace(searchRegExp, replaceWith);
result; // => 'goose goose go'
4 giải quyết vấn đề.Tuy nhiên, nó có đáng để thoát khỏi chuỗi tìm kiếm bằng cách sử dụng một hàm như escaperegexp () để được sử dụng làm biểu thức thông thường không? Hầu như không. 2.2 thay thế () bằng một chuỗiNếu đối số đầu tiên javascript
const resultingString = pieces.join(replace);
3 của javascript
const searchRegExp = /duck/gi;
const replaceWith = 'goose';
const result = 'DUCK Duck go'.replace(searchRegExp, replaceWith);
result; // => 'goose goose go'
6 là một chuỗi, thì phương thức chỉ thay thế lần xuất hiện đầu tiên của javascript
const resultingString = pieces.join(replace);
3:javascript
const search = ' ';
const replace = '-';
const result = 'duck duck go'.replace(search, replace);
result; // => 'duck-duck go'
javascript
const searchRegExp = /duck/gi;
const replaceWith = 'goose';
const result = 'DUCK Duck go'.replace(searchRegExp, replaceWith);
result; // => 'goose goose go'
8 chỉ thay thế sự xuất hiện đầu tiên của một không gian.3. Phương thức thay thế ()Cuối cùng, phương thức javascript
const searchRegExp = /duck/gi;
const replaceWith = 'goose';
const result = 'DUCK Duck go'.replace(searchRegExp, replaceWith);
result; // => 'goose goose go'
9 thay thế tất cả các lần xuất hiện của chuỗi javascript
const resultingString = pieces.join(replace);
3 bằng javascript
const search = ' ';
const replaceWith = '-';
const result = 'duck duck go'.split(search).join(replaceWith);
result; // => 'duck-duck-go'
5.Hãy thay thế tất cả các lần xuất hiện của javascript
const resultingString = pieces.join(replace);
5 bằng javascript
const resultingString = pieces.join(replace);
6:javascript
const search = ' ';
const replaceWith = '-';
const result = 'duck duck go'.replaceAll(search, replaceWith);
result; // => 'duck-duck-go'
Biểu thức thông thường theo nghĩa đen javascript
function replaceAll(string, search, replace) {
return string.split(search).join(replace);
}
replaceAll('abba', 'a', 'i'); // => 'ibbi'
replaceAll('go go go!', 'go', 'move'); // => 'move move move!'
replaceAll('oops', 'z', 'y'); // => 'oops'
3 (lưu ý cờ toàn cầu javascript
const search = ' ';
const replaceWith = '-';
const result = 'duck duck go'.split(search).join(replaceWith);
result; // => 'duck-duck-go'
7) khớp với không gian javascript
const resultingString = pieces.join(replace);
5.javascript
function replaceAll(string, search, replace) {
return string.split(search).join(replace);
}
replaceAll('abba', 'a', 'i'); // => 'ibbi'
replaceAll('go go go!', 'go', 'move'); // => 'move move move!'
replaceAll('oops', 'z', 'y'); // => 'oops'
6 thay thế tất cả các trận đấu của javascript
function replaceAll(string, search, replace) {
return string.split(search).join(replace);
}
replaceAll('abba', 'a', 'i'); // => 'ibbi'
replaceAll('go go go!', 'go', 'move'); // => 'move move move!'
replaceAll('oops', 'z', 'y'); // => 'oops'
3 bằng javascript
const resultingString = pieces.join(replace);
6, dẫn đến javascript
const search = ' ';
const replaceWith = '-';
const result = 'duck duck go'.split(search).join(replaceWith);
result; // => 'duck-duck-go'
2.Bạn có thể dễ dàng thay thế trường hợp không nhạy cảm bằng cách thêm cờ javascript
const searchRegExp = /\s/g;
const replaceWith = '-';
const result = 'duck duck go'.replace(searchRegExp, replaceWith);
result; // => 'duck-duck-go'
0 vào biểu thức thông thường:Biểu thức thông thường javascriptconst searchRegExp = /\s/g;const replaceWith = '-';const result = 'duck duck go'.replace(searchRegExp, replaceWith);result; // => 'duck-duck-go'1 thực hiện tìm kiếm không nhạy cảm với trường hợp toàn cầu (lưu ý javascriptconst searchRegExp = /\s/g;const replaceWith = '-';const result = 'duck duck go'.replace(searchRegExp, replaceWith);result; // => 'duck-duck-go'0 và javascriptconst search = ' ';const replaceWith = '-';const result = 'duck duck go'.split(search).join(replaceWith);result; // => 'duck-duck-go'7 cờ). javascriptconst searchRegExp = /\s/g;const replaceWith = '-';const result = 'duck duck go'.replace(searchRegExp, replaceWith);result; // => 'duck-duck-go'1 khớp với javascriptconst searchRegExp = /\s/g;const replaceWith = '-';const result = 'duck duck go'.replace(searchRegExp, replaceWith);result; // => 'duck-duck-go'5, cũng như javascriptconst searchRegExp = /\s/g;const replaceWith = '-';const result = 'duck duck go'.replace(searchRegExp, replaceWith);result; // => 'duck-duck-go'6.Gọi javascript
const searchRegExp = /\s/g;
const replaceWith = '-';
const result = 'duck duck go'.replace(searchRegExp, replaceWith);
result; // => 'duck-duck-go'
7 thay thế tất cả các trận đấu của các chuỗi con javascript
const searchRegExp = /\s/g;
const replaceWith = '-';
const result = 'duck duck go'.replace(searchRegExp, replaceWith);
result; // => 'duck-duck-go'
1 với javascript
const searchRegExp = /\s/g;
const replaceWith = '-';
const result = 'duck duck go'.replace(searchRegExp, replaceWith);
result; // => 'duck-duck-go'
9.- 2.1 Biểu thức chính quy từ chuỗi
- Khi biểu thức chính quy được tạo từ một chuỗi, bạn phải thoát khỏi các ký tự
javascript
const searchRegExp = /duck/gi;
const replaceWith = 'goose';
const result = 'DUCK Duck go'.replace(searchRegExp, replaceWith);
result; // => 'goose goose go'
0 vì chúng có ý nghĩa đặc biệt trong biểu thức thông thường.
Do đó, các ký tự đặc biệt là một vấn đề khi bạn muốn thay thế tất cả các hoạt động. Đây là một ví dụ:Đoạn trích ở trên cố gắng chuyển đổi chuỗi tìm kiếm javascript
const searchRegExp = /duck/gi;
const replaceWith = 'goose';
const result = 'DUCK Duck go'.replace(searchRegExp, replaceWith);
result; // => 'goose goose go'
1 thành một biểu thức thông thường. Nhưng javascript
const searchRegExp = /duck/gi;
const replaceWith = 'goose';
const result = 'DUCK Duck go'.replace(searchRegExp, replaceWith);
result; // => 'goose goose go'
1 là một biểu thức chính quy không hợp lệ, do đó javascript
const searchRegExp = /duck/gi;
const replaceWith = 'goose';
const result = 'DUCK Duck go'.replace(searchRegExp, replaceWith);
result; // => 'goose goose go'
3 bị ném.Thoát khỏi nhân vật javascript
const searchRegExp = /duck/gi;
const replaceWith = 'goose';
const result = 'DUCK Duck go'.replace(searchRegExp, replaceWith);
result; // => 'goose goose go'
4 giải quyết vấn đề.Thật không may, bạn không thể dễ dàng tạo các biểu thức chính quy từ một chuỗi trong thời gian chạy, bởi vì các ký tự đặc biệt của các biểu thức chính quy phải được thoát ra. Và đối phó với một biểu thức thường xuyên cho một sự thay thế đơn giản của các chuỗi là quá sức. Cuối cùng, phương thức chuỗi mới javascript
const searchRegExp = /duck/gi;
const replaceWith = 'goose';
const result = 'DUCK Duck go'.replace(searchRegExp, replaceWith);
result; // => 'goose goose go'
9 thay thế tất cả các lần xuất hiện chuỗi. Phương pháp này là một đề xuất ở Giai đoạn 4, và hy vọng, nó sẽ sớm đạt được một tiêu chuẩn JavaScript mới.Đề xuất của tôi là sử dụng javascript
const search = ' ';
const replaceWith = '-';
const result = 'duck duck go'.replaceAll(search, replaceWith);
result; // => 'duck-duck-go'
1 để thay thế các chuỗi.Những cách khác để thay thế tất cả các lần xuất hiện chuỗi mà bạn biết? Hãy chia sẻ một bình luận dưới đây!
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.
Bạn có thể thay thế tất cả các lần xuất hiện?
Để làm cho phương thức thay thế () thay thế tất cả các lần xuất hiện của mẫu, bạn phải kích hoạt cờ toàn cầu trên biểu thức thông thường: nối g sau khi kết thúc biểu thức thông thường theo nghĩa đen: /search /g.Hoặc khi sử dụng hàm tạo biểu thức chính quy, hãy thêm 'g' vào đối số thứ hai: regexp mới ('search', 'g')): Append g after at the end of regular expression literal: /search/g. Or when using a regular expression constructor, add 'g' to the second argument: new RegExp('search', 'g') |