This article mainly introduces an example of the replace method for JavaScript string objects. The replace method is used for string replacement and supports regular expression replacement. For more information, see
JavaScript replace Method
The replace method is used to replace some strings with other strings, or replace strings that match the regular expression match, and return the replaced strings. The syntax is as follows:
The Code is as follows:
Str_object.replace (reg_exp/str, replacement)
Parameter description:
Parameters |
Description |
Str_object |
String (object) to be operated) |
Reg_exp/str |
Required. Regular Expression to be matched/string to be replaced If reg_exp has a global flag, the replace () method replaces all matched substrings. Otherwise, it only replaces the first matched substring. |
Replacement |
Required. String to be replaced |
String replacement instance
The following example demonstrates the string replacement instance of the replace method:
The Code is as follows: