replace()

Finds all the specified patterns in the string, replaces them with the specified string.

Syntax

replace(STR_EXPR, PATTERN, REPLACE_WITH_THIS[, REGEX_FLAG])
Required Parameter
STR_EXPR
Source string expression
PATTERN
String to search for a match. If you specify "re" as REGEX_FLAG, you can use the regular expression to search for patterns.
REPLACE_WITH_THIS
Replacement string.
Optional Parameter
REGEX_FLAG
If you provide "re" as a regular expression pattern flag, the function uses the regular expression to search for patterns.

Usage

json "{}" 
| eval new=replace("hello world", "world" , "logpresso")
  => "hello logpresso"

json "{}" 
| eval new=replace("123412345", "12" , "!")
  => "!34!345"

json "{}" 
| eval new=replace("google", "^g" , "b", "re")
  => "boogle"

json "{}" 
| eval
  new=replace(
    "A:2 B:3 C:5 hahaha A:12 B:13 C:15",
    "A:(\\d+) B:\\d+ C:(\\d+)",
    "$1 $2 \\$1", "re"
  )
  => "2 5 $1 hahaha 12 15 $1"