Để xóa tất cả các ký tự đặc biệt khỏi một chuỗi, hãy gọi phương thức
const str = 'milk and @#$%&!bread';
// 👇 No 'g' flag in regex
const noSpecialChars = str.replace[/[^a-zA-Z0-9 ]/, ''];
// 👇 Only first special character removed
console.log[noSpecialChars]; // milk and #$%&!bread
9 trên chuỗi, chuyển biểu thức chính quy danh sách trắng và một chuỗi trống làm đối số, i. e. , const str = 'milk and @#$%&!bread';
// 👇 No 'g' flag in regex
const noSpecialChars = str.replace[/[^a-zA-Z0-9 ]/, ''];
// 👇 Only first special character removed
console.log[noSpecialChars]; // milk and #$%&!bread
0. Phương thức const str = 'milk and @#$%&!bread';
// 👇 No 'g' flag in regex
const noSpecialChars = str.replace[/[^a-zA-Z0-9 ]/, ''];
// 👇 Only first special character removed
console.log[noSpecialChars]; // milk and #$%&!bread
9 sẽ trả về một chuỗi mới không chứa bất kỳ ký tự đặc biệt nàoVí dụ
const str = 'milk and @#$%&!bread';
const noSpecialChars = str.replace[/[^a-zA-Z0-9 ]/g, ''];
console.log[noSpecialChars]; // milk and bread
Phương thức
const str = 'milk and @#$%&!bread';
// 👇 No 'g' flag in regex
const noSpecialChars = str.replace[/[^a-zA-Z0-9 ]/, ''];
// 👇 Only first special character removed
console.log[noSpecialChars]; // milk and #$%&!bread
2 const str = 'milk and @#$%&!bread';
// 👇 No 'g' flag in regex
const noSpecialChars = str.replace[/[^a-zA-Z0-9 ]/, ''];
// 👇 Only first special character removed
console.log[noSpecialChars]; // milk and #$%&!bread
9 tìm kiếm một chuỗi cho một giá trị [chuỗi con hoặc biểu thức chính quy] và trả về một chuỗi mới có giá trị được thay thế bằng một chuỗi con. Nó không sửa đổi chuỗi gốcDấu ngoặc vuông trong biểu thức chính quy [
const str = 'milk and @#$%&!bread';
// 👇 No 'g' flag in regex
const noSpecialChars = str.replace[/[^a-zA-Z0-9 ]/, ''];
// 👇 Only first special character removed
console.log[noSpecialChars]; // milk and #$%&!bread
0] biểu thị một lớp ký tự. Chỉ các ký tự được đặt trong nó mới được khớpNhưng sau khi đặt dấu mũ [_______11] làm ký tự đầu tiên trong dấu ngoặc vuông, thì chỉ những ký tự không nằm trong dấu ngoặc vuông mới được khớp
Sau dấu mũ, chúng tôi chỉ định
- phạm vi cho chữ thường [
2] và chữ hoa [const str = 'milk and @#$%&!bread'; // 👇 No 'g' flag in regex const noSpecialChars = str.replace[/[^a-zA-Z0-9 ]/, '']; // 👇 Only first special character removed console.log[noSpecialChars]; // milk and #$%&!bread
3]const str = 'milk and @#$%&!bread'; // 👇 No 'g' flag in regex const noSpecialChars = str.replace[/[^a-zA-Z0-9 ]/, '']; // 👇 Only first special character removed console.log[noSpecialChars]; // milk and #$%&!bread
- một phạm vi cho các chữ số từ
4const str = 'milk and @#$%&!bread'; // 👇 No 'g' flag in regex const noSpecialChars = str.replace[/[^a-zA-Z0-9 ]/, '']; // 👇 Only first special character removed console.log[noSpecialChars]; // milk and #$%&!bread
- ký tự khoảng trắng [______15]
Vì vậy, biểu thức chính quy khớp với bất kỳ ký tự nào không phải là chữ thường hoặc chữ in hoa, chữ số hoặc khoảng trắng và phương thức
const str = 'milk and @#$%&!bread';
// 👇 No 'g' flag in regex
const noSpecialChars = str.replace[/[^a-zA-Z0-9 ]/, ''];
// 👇 Only first special character removed
console.log[noSpecialChars]; // milk and #$%&!bread
9 trả về một chuỗi mới với tất cả các ký tự này đã bị xóa khỏi chuỗi ban đầuCờ
const str = 'milk and @#$%&!bread';
// 👇 No 'g' flag in regex
const noSpecialChars = str.replace[/[^a-zA-Z0-9 ]/, ''];
// 👇 Only first special character removed
console.log[noSpecialChars]; // milk and #$%&!bread
7 [toàn cầu] chỉ định rằng mọi lần xuất hiện của mẫu phải được khớpNếu chúng ta không vượt qua cờ toàn cầu, chỉ ký tự đặc biệt đầu tiên trong chuỗi sẽ được khớp và xóa
const str = 'milk and @#$%&!bread';
// 👇 No 'g' flag in regex
const noSpecialChars = str.replace[/[^a-zA-Z0-9 ]/, ''];
// 👇 Only first special character removed
console.log[noSpecialChars]; // milk and #$%&!bread
Đăng ký bản tin Coding Beauty
Có được những hiểu biết hữu ích và nâng cao kiến thức phát triển web của bạn với các mẹo và hướng dẫn hàng tuần từ Coding Beauty. Hơn 1.400 nhà phát triển đăng ký
Rút gọn biểu thức chính quy với ký tự const str = 'milk and @#$%&!bread';
// 👇 No 'g' flag in regex
const noSpecialChars = str.replace[/[^a-zA-Z0-9 ]/, ''];
// 👇 Only first special character removed
console.log[noSpecialChars]; // milk and #$%&!bread
8
const str = 'milk and @#$%&!bread';
// 👇 No 'g' flag in regex
const noSpecialChars = str.replace[/[^a-zA-Z0-9 ]/, ''];
// 👇 Only first special character removed
console.log[noSpecialChars]; // milk and #$%&!bread
Chúng ta có thể rút ngắn biểu thức chính quy này một chút bằng ký tự
const str = 'milk and @#$%&!bread';
// 👇 No 'g' flag in regex
const noSpecialChars = str.replace[/[^a-zA-Z0-9 ]/, ''];
// 👇 Only first special character removed
console.log[noSpecialChars]; // milk and #$%&!bread
8const str = 'milk and @#$%&!bread';
// 👇 No 'g' flag in regex
const noSpecialChars = str.replace[/[^a-zA-Z0-9 ]/, ''];
// 👇 Only first special character removed
console.log[noSpecialChars]; // milk and #$%&!bread
1Ký tự
const str = 'milk and @#$%&!bread';
// 👇 No 'g' flag in regex
const noSpecialChars = str.replace[/[^a-zA-Z0-9 ]/, ''];
// 👇 Only first special character removed
console.log[noSpecialChars]; // milk and #$%&!bread
8 khớp với các chữ cái Latinh viết hoa và viết thường, chữ số và dấu gạch dướiVì
const str = 'milk and @#$%&!bread';
// 👇 No 'g' flag in regex
const noSpecialChars = str.replace[/[^a-zA-Z0-9 ]/, ''];
// 👇 Only first special character removed
console.log[noSpecialChars]; // milk and #$%&!bread
8 khớp với dấu gạch dưới, nên không thể sử dụng riêng nó để xóa ký tự đặc biệt này khỏi chuỗiconst str = 'milk and @#$%&!bread';
// 👇 No 'g' flag in regex
const noSpecialChars = str.replace[/[^a-zA-Z0-9 ]/, ''];
// 👇 Only first special character removed
console.log[noSpecialChars]; // milk and #$%&!bread
4Chúng tôi sẽ cần một biểu thức chính quy khác để xóa dấu gạch dưới
const str = 'milk and @#$%&!bread';
// 👇 No 'g' flag in regex
const noSpecialChars = str.replace[/[^a-zA-Z0-9 ]/, ''];
// 👇 Only first special character removed
console.log[noSpecialChars]; // milk and #$%&!bread
5Ký hiệu ống [
const str = 'milk and @#$%&!bread';
// 👇 No 'g' flag in regex
const noSpecialChars = str.replace[/[^a-zA-Z0-9 ]/, ''];
// 👇 Only first special character removed
console.log[noSpecialChars]; // milk and #$%&!bread
12] cho phép một trong hai mẫu được khớp với nhau trong một chuỗi, tương tự như một lớp ký tự. Để sử dụng biểu tượng đường ống, chúng ta cần đặt hai mẫu trong ngoặc đơn [const str = 'milk and @#$%&!bread';
// 👇 No 'g' flag in regex
const noSpecialChars = str.replace[/[^a-zA-Z0-9 ]/, ''];
// 👇 Only first special character removed
console.log[noSpecialChars]; // milk and #$%&!bread
13 và const str = 'milk and @#$%&!bread';
// 👇 No 'g' flag in regex
const noSpecialChars = str.replace[/[^a-zA-Z0-9 ]/, ''];
// 👇 Only first special character removed
console.log[noSpecialChars]; // milk and #$%&!bread
14], đó là những gì chúng ta đã làm11 Tính năng JavaScript mới tuyệt vời trong ES13
Hướng dẫn này sẽ giúp bạn cập nhật tất cả các tính năng mới nhất được thêm vào trong ECMAScript 13. Những tính năng mới mạnh mẽ này sẽ hiện đại hóa JavaScript của bạn bằng mã ngắn hơn và biểu cảm hơn
Đăng ký và nhận ngay một bản sao miễn phí
Ayibatari Ibaba
Ayibatari Ibaba là nhà phát triển phần mềm có nhiều năm kinh nghiệm xây dựng trang web và ứng dụng. Anh ấy đã viết rất nhiều về nhiều chủ đề lập trình và đã tạo ra hàng chục ứng dụng và thư viện mã nguồn mở