2020-03-10 11:01:18 -04:00
|
|
|
---
|
2020-03-18 18:46:47 -04:00
|
|
|
layout: docs
|
|
|
|
page_title: regexreplace - Functions - Configuration Language
|
2020-04-02 19:39:47 -04:00
|
|
|
sidebar_title: regexreplace
|
2020-03-10 11:01:18 -04:00
|
|
|
description: |-
|
|
|
|
The regexreplace function searches a given string for another given substring,
|
|
|
|
and replaces all occurrences with a given replacement string. The substring
|
|
|
|
argument can be a valid regular expression or a string.
|
|
|
|
---
|
|
|
|
|
|
|
|
# `regexreplace` Function
|
|
|
|
|
|
|
|
`regexreplace` searches a given string for another given substring, and
|
|
|
|
replaces each occurrence with a given replacement string. The substring
|
|
|
|
argument can be a valid regular expression or a string.
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
regexreplace(string, substring, replacement)
|
|
|
|
```
|
|
|
|
|
|
|
|
`substring` should not be wrapped in forward slashes, it is always treated as a
|
|
|
|
regular expression. The `replacement` string can incorporate captured strings
|
|
|
|
from the input by using an `$n` or `${n}` sequence, where `n` is the index or
|
|
|
|
name of a capture group.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2020-03-31 18:47:06 -04:00
|
|
|
```text
|
2020-03-10 11:01:18 -04:00
|
|
|
> regexreplace("hello world", "world", "everybody")
|
|
|
|
hello everybody
|
|
|
|
|
|
|
|
|
|
|
|
> regexreplace("hello world", "w.*d", "everybody")
|
|
|
|
hello everybody
|
|
|
|
|
|
|
|
> regexreplace("-ab-axxb-", "a(x*)b", "$1W)
|
|
|
|
---
|
|
|
|
|
|
|
|
> regexreplace("-ab-axxb-", "a(x*)b", "${1}W")
|
|
|
|
-W-xxW-
|
|
|
|
```
|
|
|
|
|
|
|
|
## Related Functions
|
|
|
|
|
2020-04-01 15:15:54 -04:00
|
|
|
- [`replace`](/docs/from-1.5/functions/string/replace) searches a given string for another given
|
2020-03-10 11:01:18 -04:00
|
|
|
substring, and replaces all occurrences with a given replacement string.
|