String.replace

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

Parameters

ParametersTypeRequiredDefault valueDescription
fromString/RegexNo''The string or regular expression to replace.
toAnyNo''The replaced content will be converted to a string first when called.

Return value

TypeDescription
StringReturns the new string after replacement.

Example

Notes

  • String patterns use a single replacement.
  • Whether the regular pattern is replaced globally is determined by the g flag of Regex.