Skip to content

Regex Replace

The regex::replace function replaces all substrings that match a given regular expression pattern with a specified replacement string.

Syntax

regex::replace(<string>, <pattern>, <replacement>)

Parameters

  • string: The input string in which matches are to be replaced.
  • pattern: The regular expression pattern to match against.
  • replacement: The string that will replace all matches.

Returns

  • String: A new string with the matches replaced.

Example

Try it out below!
regex::replace("This is a test.", "\\btest\\b", "replacement")

This function is useful for modifying strings based on patterns.