String.replace
String.replace
Function
Replaces matching content in a string. Only the first occurrence of a string pattern is replaced; all matches are replaced when the Regex parameter has the g flag.
Syntax
string.replace(from, to)
Parameters
| Parameters | Type | Required | Default value | Description |
|---|---|---|---|---|
| from | String/Regex | No | '' | The string or regular expression to replace. |
| to | Any | No | '' | The replaced content will be converted to a string first when called. |
Return value
| Type | Description |
|---|---|
| String | Returns the new string after replacement. |
Example
text = 'hello js' result = text.replace('js', 'BT') // Output: hello BT print result
Notes
- String patterns use a single replacement.
- Whether the regular pattern is replaced globally is determined by the g flag of Regex.