Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận The replace method can be used to replace the specified string with another string. It takes two parameters, first is the string to be replaced and the second is the string which is replacing from the first string. The second string can be given an empty string so that the text to be replaced is removed. This method however only removes the first occurrence of the string.
Syntax:
string.replace['textToReplace', ''];
Có ba phương thức để xóa văn bản khỏi một chuỗi được liệt kê dưới đây: This example replace the first occurrence of string.
string.replace[/regExp/g, ''];0
string.replace[/regExp/g, ''];4
string.replace[/regExp/g, ''];5
string.replace[/regExp/g, ''];4
string.replace[/regExp/g, ''];7
string.replace[/regExp/g, ''];0
string.replace[/regExp/g, ''];9
string.replace[/regExp/g, ''];2
>
string.replace[/regExp/g, ''];9
head
>
Phương pháp 1: Sử dụng phương thức thay thế []: Phương thức thay thế có thể được sử dụng để thay thế chuỗi được chỉ định bằng một chuỗi khác. Phải mất hai tham số, đầu tiên là chuỗi được thay thế và thứ hai là chuỗi được thay thế từ chuỗi đầu tiên. Chuỗi thứ hai có thể được cung cấp một chuỗi trống để văn bản được thay thế được loại bỏ. Tuy nhiên, phương pháp này chỉ loại bỏ sự xuất hiện đầu tiên của chuỗi.
string.replace[/regExp/g, ''];4
6Ví dụ: Ví dụ này thay thế lần xuất hiện đầu tiên của chuỗi.
string.replace[/regExp/g, ''];0
4___string.replace[/regExp/g, ''];4
>
7>
8>
9
>
8
string.replace[/regExp/g, ''];01
>
8
string.replace[/regExp/g, ''];03
string.replace[/regExp/g, ''];04
string.replace[/regExp/g, ''];05
string.replace[/regExp/g, ''];4
string.replace[/regExp/g, ''];07
string.replace[/regExp/g, ''];0
string.replace[/regExp/g, ''];9
>
1>
string.replace[/regExp/g, ''];9
string.substr[start, length];6
>
string.replace[/regExp/g, ''];9
html
string.replace[/regExp/g, ''];17
Output:
____107
>
8>
9
>
8
string.substr[start, length];16
>
8
string.replace[/regExp/g, ''];03
string.replace[/regExp/g, ''];04
string.replace[/regExp/g, ''];05
string.replace[/regExp/g, ''];4
string.replace[/regExp/g, ''];07
string.replace[/regExp/g, ''];0
string.replace[/regExp/g, ''];9
>
1>
string.replace[/regExp/g, ''];9
string.substr[start, length];6
>
string.replace[/regExp/g, ''];9
html
string.replace[/regExp/g, ''];17
Output:
____108
>
9
>
831
>
8
string.replace[/regExp/g, ''];03
string.replace[/regExp/g, ''];04
string.replace[/regExp/g, ''];05
string.replace[/regExp/g, ''];4
string.replace[/regExp/g, ''];07
string.replace[/regExp/g, ''];0
string.replace[/regExp/g, ''];9
>
1string.replace[/regExp/g, ''];9
string.substr[start, length];6
>
string.replace[/regExp/g, ''];9
html
string.replace[/regExp/g, ''];17
Output: