Khi làm việc với một chương trình JavaScript, bạn có thể cần thay thế một ký tự hoặc từ bằng một ký tự hoặc từ khác
Cụ thể, bạn có thể cần thay thế không chỉ một mà tất cả các lần xuất hiện của ký tự hoặc từ đó bằng một từ khác
Có một số cách bạn có thể đạt được điều này với JavaScript
Một trong những cách đó là sử dụng phương pháp
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
4 tích hợp sẵn mà bạn sẽ học cách sử dụng trong bài viết nàyĐây là những gì chúng tôi sẽ đề cập
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
4 trong JavaScript là gì?
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
Phương thức
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
4 là một phần của thư viện chuẩn của JavaScript. Khi bạn sử dụng nó, bạn thay thế tất cả các trường hợp của một chuỗiCó nhiều cách khác nhau để bạn có thể thay thế tất cả các phiên bản của chuỗi. Điều đó nói rằng, sử dụng
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
4 là cách đơn giản nhất và nhanh nhất để làm như vậyMột điều cần lưu ý là chức năng này đã được giới thiệu với ES2021
Mặc dù phương pháp
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
4 tương thích với tất cả các trình duyệt chính, nhưng nó không phải là giải pháp tốt nhất khi phát triển cho các phiên bản trình duyệt cũ hơn, vì các phiên bản cũ hơn này có thể không hiểu và hỗ trợ nóPhương pháp const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
4 - Phân tích cú pháp
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
Cú pháp chung của phương thức
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
4 trông như thế nàystring.replaceAll[pattern, replacement]
Hãy phá vỡ nó
7 là chuỗi ban đầu bạn đang làm việc và chuỗi bạn sẽ gọi phương thứcconsole.log[my_new_string]; // I like cats because cats are adorable!
4 trênconst my_string = "I like dogs because dogs are adorable!"; let pattern = "dogs"; let replacement = "cats"; let my_new_string = my_string.replaceAll[pattern,replacement];
- Phương thức
4 nhận 2 tham sốconst my_string = "I like dogs because dogs are adorable!"; let pattern = "dogs"; let replacement = "cats"; let my_new_string = my_string.replaceAll[pattern,replacement];
10 là tham số đầu tiên, có thể là chuỗi con hoặc biểu thức chính quy - tham số này đề cập đến mục bạn muốn thay đổi và thay thế bằng mục khácconst my_string = "I like dogs because dogs are adorable!"; let pattern = "dogs"; let replacement = "cats"; let my_new_string = my_string.replaceAll[pattern,replacement];
- Nếu
10 là một biểu thức chính quy, bạn cần bao gồm cờconst my_string = "I like dogs because dogs are adorable!"; let pattern = "dogs"; let replacement = "cats"; let my_new_string = my_string.replaceAll[pattern,replacement];
12 [trong đóconst my_string = "I like dogs because dogs are adorable!"; let pattern = "dogs"; let replacement = "cats"; let my_new_string = my_string.replaceAll[pattern,replacement];
12 là viết tắt củaconst my_string = "I like dogs because dogs are adorable!"; let pattern = "dogs"; let replacement = "cats"; let my_new_string = my_string.replaceAll[pattern,replacement];
12lobal] hoặcconst my_string = "I like dogs because dogs are adorable!"; let pattern = "dogs"; let replacement = "cats"; let my_new_string = my_string.replaceAll[pattern,replacement];
4 sẽ đưa ra một ngoại lệ - cụ thể, lỗi sẽ làconst my_string = "I like dogs because dogs are adorable!"; let pattern = "dogs"; let replacement = "cats"; let my_new_string = my_string.replaceAll[pattern,replacement];
16const my_string = "I like dogs because dogs are adorable!"; let pattern = "dogs"; let replacement = "cats"; let my_new_string = my_string.replaceAll[pattern,replacement];
- Nếu
17 là tham số thứ hai, có thể là một chuỗi khác hoặc một hàm thay thế choconst my_string = "I like dogs because dogs are adorable!"; let pattern = "dogs"; let replacement = "cats"; let my_new_string = my_string.replaceAll[pattern,replacement];
10const my_string = "I like dogs because dogs are adorable!"; let pattern = "dogs"; let replacement = "cats"; let my_new_string = my_string.replaceAll[pattern,replacement];
Một điều cần lưu ý ở đây là phương thức
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
4 không thay đổi chuỗi gốc. Thay vào đó, nó trả về một bản sao mớiTất cả các phiên bản của
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
10 được chỉ định sẽ được thay thế bằng const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
17Cách sử dụng const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
4 với Chuỗi làm tham số đầu tiên Ví dụ
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
Trước đó, bạn đã thấy phương thức
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
4 chấp nhận hai tham số - const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
10 là tham số đầu tiên và const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
17 là tham số thứ haiBạn cũng thấy rằng
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
10 có thể là một chuỗi hoặc một biểu thức chính quyBây giờ, hãy xem cách hoạt động của
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
4 khi nó lấy một chuỗi làm tham số đầu tiênVì vậy, giả sử bạn có ví dụ sau
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
Tôi lưu trữ văn bản
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
08 trong một biến có tên là const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
09Đây là chuỗi gốc mà tôi đang làm việc và tôi muốn sửa đổi một số nội dung của nó
Cụ thể, tôi muốn thay đổi chuỗi con
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
20, xuất hiện hai lần trong chuỗi gốc - đây sẽ là const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
10 của tôiTôi lưu trữ chuỗi con này mà tôi muốn thay thế bằng một thứ khác trong một biến có tên là
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
10Sau đó, tôi lưu trữ chuỗi
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
23 trong một biến có tên là const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
17 - đây là chuỗi sẽ thay thế cho const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
20Sau đó, tôi gọi phương thức
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
4 trên chuỗi ban đầu, chuyển hai chuỗi con làm tham số và lưu kết quả đó vào một biến có tên là const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
27________số 8Phương thức
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
4 sẽ thay thế tất cả các phiên bản của chuỗi con const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
20 trong chuỗi const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
08 bằng const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
23Chuỗi ban đầu sẽ không thay đổi
Một điều cần lưu ý ở đây là sự thay thế khi sử dụng một chuỗi làm tham số đầu tiên phân biệt chữ hoa chữ thường. Điều này có nghĩa là chỉ chuỗi có cùng trường hợp khớp với
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
10 mới được thay thếconst my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
1Trong ví dụ trên, có hai trường hợp của
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
20, nhưng trường hợp đầu tiên có số vốn là const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
84Như bạn có thể thấy ở đầu ra, sự thay thế phân biệt chữ hoa chữ thường
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
0Cách sử dụng const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
4 với Biểu thức chính quy làm Tham số đầu tiên Ví dụ
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
Như bạn đã thấy trước đó, bạn có thể chuyển một biểu thức chính quy [còn được gọi là biểu thức chính quy] làm tham số đầu tiên
Biểu thức chính quy là một chuỗi các ký tự tạo mẫu tìm kiếm
Cú pháp chung để làm điều này tương tự như sau
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
2Hãy lấy ví dụ từ phần trước và thay vì một chuỗi, hãy sử dụng biểu thức chính quy làm tham số đầu tiên
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
8Khi sử dụng biểu thức chính quy làm tham số đầu tiên, hãy đảm bảo sử dụng cờ
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
12Nếu không, cuối cùng bạn sẽ gặp lỗi trong mã của mình
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
4Hãy tinh chỉnh chuỗi ban đầu một chút
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
5Bây giờ tôi đã có hai trường hợp của
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
20, nhưng một trong số đó là với một const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
84 viết hoaTôi kết thúc với đầu ra sau
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
0Từ đầu ra đó, bạn có thể biết rằng đó là sự thay thế phân biệt chữ hoa chữ thường
Để phân biệt chữ hoa chữ thường, hãy đảm bảo thêm cờ
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
89 sau cờ const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
12 như vậyconst my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
0Biểu thức chính quy
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
41 sẽ khớp với tất cả các trường hợp chứa chuỗi con đó và làm cho trường hợp thay thế không nhạy cảmPhương pháp const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
4 so với phương pháp console.log[my_new_string];
// I like cats because cats are adorable!
0 - Đâu là sự khác biệt?
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
console.log[my_new_string];
// I like cats because cats are adorable!
Sự khác biệt giữa các phương pháp
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
4 và console.log[my_new_string];
// I like cats because cats are adorable!
0 là const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
4 thực hiện thay thế toàn cục ngay lập tứcPhương thức
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
4 sẽ thay thế tất cả các phiên bản của chuỗi hoặc mẫu biểu thức chính quy mà bạn chỉ định, trong khi phương thức console.log[my_new_string];
// I like cats because cats are adorable!
0 sẽ chỉ thay thế lần xuất hiện đầu tiênĐây là cách
console.log[my_new_string];
// I like cats because cats are adorable!
0 hoạt động với một chuỗi làm tham số đầu tiênconst my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
1Và đây là cách
console.log[my_new_string];
// I like cats because cats are adorable!
0 hoạt động với biểu thức chính quy làm tham số đầu tiênconst my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
2Cách duy nhất để thực hiện thay thế toàn cục bằng phương thức
console.log[my_new_string];
// I like cats because cats are adorable!
0 là sử dụng biểu thức chính quy với cờ const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
12const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
3Phần kết luận
Và bạn có nó rồi đấy. Bây giờ bạn đã biết phương pháp
const my_string = "I like dogs because dogs are adorable!";
let pattern = "dogs";
let replacement = "cats";
let my_new_string = my_string.replaceAll[pattern,replacement];
4 hoạt động như thế nào và một số cách bạn có thể sử dụng nóĐể tìm hiểu thêm về JavaScript, hãy đến với Chứng nhận cấu trúc dữ liệu và thuật toán JavaScript của freeCodeCamp
Đó là một chương trình giảng dạy miễn phí, được cân nhắc kỹ lưỡng và có cấu trúc, nơi bạn sẽ học một cách tương tác. Cuối cùng, bạn cũng sẽ xây dựng 5 dự án để nhận chứng chỉ và củng cố kiến thức của mình
Cảm ơn vì đã đọc
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
Học một cái gì đó mới mỗi ngày và viết về nó
Nếu bài viết này hữu ích, hãy tweet nó
Học cách viết mã miễn phí. Chương trình giảng dạy mã nguồn mở của freeCodeCamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu