Files
opentf/website/docs/configuration/functions/replace.html.md
Martin Atkins 135afaeb9c lang: "regex" and "regexall" functions
These existing upstream cty functions allow matching strings against
regular expression patterns, which can be useful if you need to consume
a non-standard string format that Terraform doesn't (and can't) have a
built-in function for.
2019-08-06 11:52:14 -07:00

1.3 KiB

layout, page_title, sidebar_current, description
layout page_title sidebar_current description
functions replace - Functions - Configuration Language docs-funcs-string-replace The replace function searches a given string for another given substring, and replaces all occurrences with a given replacement string.

replace Function

-> Note: This page is about Terraform 0.12 and later. For Terraform 0.11 and earlier, see 0.11 Configuration Language: Interpolation Syntax.

replace searches a given string for another given substring, and replaces each occurrence with a given replacement string.

replace(string, substring, replacement)

If substring is wrapped in forward slashes, it is treated as a regular expression, using the same pattern syntax as regex. If using a regular expression for the substring argument, the replacement string can incorporate captured strings from the input by using an $n sequence, where n is the index or name of a capture group.

Examples

> replace("1 + 2 + 3", "+", "-")
1 - 2 - 3

> replace("hello world", "/w.*d/", "everybody")
hello everybody
  • regex searches a given string for a substring matching a given regular expression pattern.