opentofu/website/docs/language/functions/replace.mdx

38 lines
1 KiB
Text
Raw Permalink Normal View History

---
sidebar_label: replace
description: |-
The replace function searches a given string for another given substring,
2019-03-21 15:20:29 -04:00
and replaces all occurrences with a given replacement string.
---
# `replace` Function
`replace` searches a given string for another given substring, and replaces
2019-03-21 15:20:29 -04:00
each occurrence with a given replacement string.
```hcl
replace(string, substring, replacement)
```
2019-07-11 12:29:41 -04:00
If `substring` is wrapped in forward slashes, it is treated as a regular
expression, using the same pattern syntax as
[`regex`](../../language/functions/regex.mdx). 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.
2019-07-11 12:29:41 -04:00
## Examples
```
> replace("1 + 2 + 3", "+", "-")
1 - 2 - 3
2019-07-11 12:29:41 -04:00
> replace("hello world", "/w.*d/", "everybody")
hello everybody
```
## Related Functions
- [`regex`](../../language/functions/regex.mdx) searches a given string for a substring matching a
given regular expression pattern.