2018-05-07 03:32:16 +02:00
|
|
|
---
|
2020-08-15 03:51:06 +02:00
|
|
|
layout: "language"
|
2018-12-20 05:35:11 +01:00
|
|
|
page_title: "replace - Functions - Configuration Language"
|
2018-05-07 03:32:16 +02:00
|
|
|
sidebar_current: "docs-funcs-string-replace"
|
|
|
|
description: |-
|
|
|
|
The replace function searches a given string for another given substring,
|
2019-03-21 20:20:29 +01:00
|
|
|
and replaces all occurrences with a given replacement string.
|
2018-05-07 03:32:16 +02:00
|
|
|
---
|
|
|
|
|
|
|
|
# `replace` Function
|
|
|
|
|
|
|
|
`replace` searches a given string for another given substring, and replaces
|
2019-03-21 20:20:29 +01:00
|
|
|
each occurrence with a given replacement string.
|
2018-05-07 03:32:16 +02:00
|
|
|
|
|
|
|
```hcl
|
|
|
|
replace(string, substring, replacement)
|
|
|
|
```
|
|
|
|
|
2019-07-11 18:29:41 +02:00
|
|
|
If `substring` is wrapped in forward slashes, it is treated as a regular
|
2019-08-06 17:21:22 +02:00
|
|
|
expression, using the same pattern syntax as
|
|
|
|
[`regex`](./regex.html). 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 18:29:41 +02:00
|
|
|
|
2018-05-07 03:32:16 +02:00
|
|
|
## Examples
|
|
|
|
|
|
|
|
```
|
|
|
|
> replace("1 + 2 + 3", "+", "-")
|
|
|
|
1 - 2 - 3
|
2019-07-11 18:29:41 +02:00
|
|
|
|
|
|
|
> replace("hello world", "/w.*d/", "everybody")
|
|
|
|
hello everybody
|
2018-05-07 03:32:16 +02:00
|
|
|
```
|
2019-08-06 17:21:22 +02:00
|
|
|
|
|
|
|
## Related Functions
|
|
|
|
|
|
|
|
- [`regex`](./regex.html) searches a given string for a substring matching a
|
|
|
|
given regular expression pattern.
|