Xóa ký tự đặc biệt ở đầu và cuối chuỗi trong javascript
Phương thức thay thế được sử dụng để thay thế một ký tự/chuỗi cụ thể bằng ký tự/chuỗi khác. Nó nhận hai tham số, thứ nhất là chuỗi cần thay thế và thứ hai là chuỗi cần thay thế. Trong trường hợp này, tham số đầu tiên là ký tự cần xóa và tham số thứ hai có thể được cung cấp dưới dạng một chuỗi rỗng. Điều này sẽ xóa ký tự khỏi chuỗi. Phương pháp này loại bỏ sự xuất hiện đầu tiên của chuỗi Show cú pháp string.replace('characterToReplace', ''); Thí dụ. htmlstring.replace(/regExp/g, '');78 string.replace(/regExp/g, '');79 string.replace(/regExp/g, '');80 string.replace(/regExp/g, '');81
string.replace(/regExp/g, '');79 string.replace(/regExp/g, '');83 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');79____13 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');6 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');8 string.replace(/regExp/g, '');1 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');3 string.replace(/regExp/g, '');81 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');83 string.replace(/regExp/g, '');81
string.replace(/regExp/g, '');79 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);7 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');79____131 string.replace(/regExp/g, '');32 string.replace(/regExp/g, '');33 string.replace(/regExp/g, '');34 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');5____137 string.replace(/regExp/g, '');1 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');31 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');79____2785 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');6 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');790 string.replace(/regExp/g, '');1 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');785 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');79____2798 string.replace(/regExp/g, '');799 string.replace(/regExp/g, '');798 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');79____2798 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');808 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');79 string.replace(/regExp/g, '');811 string.replace(/regExp/g, '');812 string.replace(/regExp/g, '');33 string.replace(/regExp/g, '');814 string.replace(/regExp/g, '');815 string.replace(/regExp/g, '');811 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');1 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');798 string.replace(/regExp/g, '');81
string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');79____2794 string.replace(/regExp/g, '');795 string.replace(/regExp/g, '');33 string.replace(/regExp/g, '');797 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');830 string.replace(/regExp/g, '');1 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');794 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');79____2838 string.replace(/regExp/g, '');839 string.replace(/regExp/g, '');33 string.replace(/regExp/g, '');01 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');04 string.replace(/regExp/g, '');05 string.replace(/regExp/g, '');06 string.replace(/regExp/g, '');05 string.replace(/regExp/g, '');08
string.replace(/regExp/g, '');05 string.replace(/regExp/g, '');10 string.replace(/regExp/g, '');11 string.replace(/regExp/g, '');12 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');14 string.replace(/regExp/g, '');1 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');838 string.replace(/regExp/g, '');81 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);7 string.replace(/regExp/g, '');81
// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');80 string.replace(/regExp/g, '');24 đầu ra
Phương pháp 2. Sử dụng Phương thức thay thế () của JavaScript với Biểu thức chính quy Phương pháp này được sử dụng để loại bỏ tất cả các lần xuất hiện của ký tự được chỉ định, không giống như phương pháp trước đó. Một biểu thức chính quy được sử dụng thay cho chuỗi cùng với thuộc tính toàn cầu. Nó sẽ chọn mọi lần xuất hiện trong chuỗi và nó có thể bị xóa. cú pháp string.replace(/regExp/g, ''); Thí dụ. htmlstring.replace(/regExp/g, '');78 string.replace(/regExp/g, '');79 string.replace(/regExp/g, '');80 string.replace(/regExp/g, '');81
string.replace(/regExp/g, '');79 string.replace(/regExp/g, '');83 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');79____13 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');6 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');8 string.replace(/regExp/g, '');1 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');3 string.replace(/regExp/g, '');81 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');83 string.replace(/regExp/g, '');81
string.replace(/regExp/g, '');79 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);7 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');79____131 string.replace(/regExp/g, '');32 string.replace(/regExp/g, '');33 string.replace(/regExp/g, '');34 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');5____137 string.replace(/regExp/g, '');1 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');31 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');79____2785 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');6 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');790 string.replace(/regExp/g, '');1 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');785 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');79____2798 string.replace(/regExp/g, '');799 string.replace(/regExp/g, '');798 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');79____2798 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');808 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');79 string.replace(/regExp/g, '');811 string.replace(/regExp/g, '');812 string.replace(/regExp/g, '');33 string.replace(/regExp/g, '');814 string.replace(/regExp/g, '');815 string.replace(/regExp/g, '');811 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');1 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');798 string.replace(/regExp/g, '');81
string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');79____2794 string.replace(/regExp/g, '');795 string.replace(/regExp/g, '');33 string.replace(/regExp/g, '');797 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');830 string.replace(/regExp/g, '');1 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');794 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');79____2838 string.replace(/regExp/g, '');839 string.replace(/regExp/g, '');33 string.replace(/regExp/g, '');01 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');04 string.replace(/regExp/g, '');05 string.replace(/regExp/g, '');06 string.replace(/regExp/g, '');05 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);29
string.replace(/regExp/g, '');05 string.replace(/regExp/g, '');10 string.replace(/regExp/g, '');11 string.replace(/regExp/g, '');12 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');14 string.replace(/regExp/g, '');1 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');838 string.replace(/regExp/g, '');81 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);7 string.replace(/regExp/g, '');81
// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');80 string.replace(/regExp/g, '');24 đầu ra
Phương pháp 3. Xóa ký tự đầu tiên hoặc ký tự cuối cùng bằng JavaScript slice() Method Phương thức slice() được sử dụng để trích xuất các phần của chuỗi giữa các tham số đã cho. Phương thức này lấy chỉ mục bắt đầu và chỉ mục kết thúc của chuỗi và trả về chuỗi ở giữa các chỉ mục này. Nếu chỉ số kết thúc không được chỉ định, thì nó được coi là độ dài của chuỗi. Có thể xóa ký tự đầu tiên bằng cách chỉ định chỉ mục bắt đầu là 1. Nó trích xuất chuỗi từ ký tự thứ hai cho đến cuối chuỗi. Có thể xóa ký tự cuối cùng bằng cách chỉ định chỉ số kết thúc nhỏ hơn độ dài của chuỗi một ký tự. Điều này trích xuất chuỗi từ đầu chuỗi đến ký tự thứ hai đến ký tự cuối cùng cú pháp ________số 8Thí dụ. htmlstring.replace(/regExp/g, '');78 string.replace(/regExp/g, '');79 string.replace(/regExp/g, '');80 string.replace(/regExp/g, '');81
string.replace(/regExp/g, '');79 string.replace(/regExp/g, '');83 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');79____13 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');6 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');8 string.replace(/regExp/g, '');1 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');3 string.replace(/regExp/g, '');81 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');83 string.replace(/regExp/g, '');81
string.replace(/regExp/g, '');79 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);7 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');79____131 string.replace(/regExp/g, '');32 string.replace(/regExp/g, '');33 string.replace(/regExp/g, '');34 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');5____137 string.replace(/regExp/g, '');1 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');31 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');79____2785 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');6 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');790 string.replace(/regExp/g, '');1 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');785 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');79____2798 string.replace(/regExp/g, '');799 string.replace(/regExp/g, '');798 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');79____2798 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');310 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');79 string.replace(/regExp/g, '');811 string.replace(/regExp/g, '');812 string.replace(/regExp/g, '');33 string.replace(/regExp/g, '');316 string.replace(/regExp/g, '');815 string.replace(/regExp/g, '');811 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');1 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');798 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');79____2798 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');5____1330 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');79 string.replace(/regExp/g, '');811 string.replace(/regExp/g, '');812 string.replace(/regExp/g, '');33 string.replace(/regExp/g, '');336 string.replace(/regExp/g, '');815 string.replace(/regExp/g, '');811 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');1 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');798 string.replace(/regExp/g, '');81
string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');79____2794 string.replace(/regExp/g, '');795 string.replace(/regExp/g, '');33 string.replace(/regExp/g, '');797 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');830 string.replace(/regExp/g, '');1 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');794 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');79____2838 string.replace(/regExp/g, '');839 string.replace(/regExp/g, '');33 string.replace(/regExp/g, '');01 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');04 string.replace(/regExp/g, '');05 string.replace(/regExp/g, '');06 string.replace(/regExp/g, '');05 string.replace(/regExp/g, '');370 string.replace(/regExp/g, '');05 string.replace(/regExp/g, '');05 string.replace(/regExp/g, '');373 string.replace(/regExp/g, '');374 string.replace(/regExp/g, '');375
string.replace(/regExp/g, '');05 string.replace(/regExp/g, '');377 string.replace(/regExp/g, '');11 string.replace(/regExp/g, '');379 string.replace(/regExp/g, '');05 string.replace(/regExp/g, '');381 string.replace(/regExp/g, '');11 string.replace(/regExp/g, '');383 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');14 string.replace(/regExp/g, '');1 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');838 string.replace(/regExp/g, '');81 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);7 string.replace(/regExp/g, '');81
// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');80 string.replace(/regExp/g, '');24 đầu ra
Phương pháp 4. Xóa một ký tự cụ thể tại chỉ mục đã cho bằng phương thức JavaScript substr(). Phương pháp này có thể được sử dụng để xóa một ký tự khỏi một chỉ mục cụ thể trong chuỗi. Phương thức substr() được sử dụng để trích xuất các phần của chuỗi giữa các tham số đã cho. Phương thức này nhận 2 tham số, một là chỉ số bắt đầu và một là chỉ số kết thúc của chuỗi. Chuỗi giữa các chỉ số này được trả về. Phần chuỗi trước và sau ký tự cần loại bỏ được tách ra và nối với nhau. Thao tác này sẽ xóa ký tự khỏi chỉ mục cụ thể cú pháp string.replace(/regExp/g, '');3 Thí dụ. htmlstring.replace(/regExp/g, '');78 string.replace(/regExp/g, '');79 string.replace(/regExp/g, '');80 string.replace(/regExp/g, '');81
string.replace(/regExp/g, '');79 string.replace(/regExp/g, '');83 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');79____13 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');6 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');8 string.replace(/regExp/g, '');1 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');3 string.replace(/regExp/g, '');81 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');83 string.replace(/regExp/g, '');81
string.replace(/regExp/g, '');79 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);7 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');79____131 string.replace(/regExp/g, '');32 string.replace(/regExp/g, '');33 string.replace(/regExp/g, '');34 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');5____137 string.replace(/regExp/g, '');1 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');31 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');79____2785 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');6 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');790 string.replace(/regExp/g, '');1 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');785 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');79____2798 string.replace(/regExp/g, '');799 string.replace(/regExp/g, '');798 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');79____2798 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');808 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');79 string.replace(/regExp/g, '');811 string.replace(/regExp/g, '');812 string.replace(/regExp/g, '');33 string.replace(/regExp/g, '');814 string.replace(/regExp/g, '');815 string.replace(/regExp/g, '');811 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');1 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');798 string.replace(/regExp/g, '');81
string.replace(/regExp/g, '');1____27875 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');79____2794 string.replace(/regExp/g, '');795 string.replace(/regExp/g, '');33 string.replace(/regExp/g, '');7881 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');830 string.replace(/regExp/g, '');1 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');794 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');1 string.replace(/regExp/g, '');79____2838 string.replace(/regExp/g, '');839 string.replace(/regExp/g, '');33 string.replace(/regExp/g, '');01 string.replace(/regExp/g, '');81 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');7898 string.replace(/regExp/g, '');05 string.replace(/regExp/g, '');06 string.replace(/regExp/g, '');05 string.replace(/regExp/g, '');05 string.replace(/regExp/g, '');7903 string.replace(/regExp/g, '');05 string.replace(/regExp/g, '');7905
string.replace(/regExp/g, '');05 string.replace(/regExp/g, '');7907 string.replace(/regExp/g, '');5 string.replace(/regExp/g, '');14 string.replace(/regExp/g, '');1 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');838 string.replace(/regExp/g, '');81 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);7 string.replace(/regExp/g, '');81
// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0 string.replace(/regExp/g, '');80 string.replace(/regExp/g, '');24 đầu ra
JavaScript được biết đến nhiều nhất để phát triển trang web nhưng nó cũng được sử dụng trong nhiều môi trường không có trình duyệt. Bạn có thể học JavaScript từ đầu bằng cách làm theo Hướng dẫn JavaScript và Ví dụ về JavaScript này |