replace_pattern
Replaces every match of an RE2 pattern in the string, with a replacement that may reference capture groups as $1, $2, and so on.
An empty replacement deletes each match, and an invalid pattern is an error.
InputArgumentsReturns
SourcestringstringstringstringBehaviors
- Coerces scalars to text
- A scalar value such as a number or a boolean is converted to its text form before matching, so
42is accepted as"42".
BasicSource
Replaces every match of an RE2 pattern, and the replacement may reference capture groups with $1 and $2.
Given
{"name": "Doe, Jane"}Template
{{ name | replace_pattern:'(\w+), (\w+)','$2 $1' }}Result
Jane DoeCollapseSource
Rewrites each run of whitespace to a single underscore.
Given
{"text": "a b c"}Template
{{ text | replace_pattern:'\s+','_' }}Result
a_b_cDeleteSource
Removes every match when the replacement is empty.
Given
{"code": "abc123def"}Template
{{ code | replace_pattern:'\d+','' }}Result
abcdef