The replace filter enables you to search and replace text patterns using Regex to modify variable output. It can also be used to conditionally wrap variable output based on a given pattern as shown in the example below.
@{ text | replace (regex, replace) }
Parameters
- regex
- A valid regular expression string, wrapped in delimiters like
"/pattern/". - replace
- The replacement string. Note that you can make use of backreferences like
$1to$nas described here. Note that you will have to use double backslashes\\to escape reserved characters in regex patterns.
Example
The following example will wrap the output of the +main variable in a div as long as it is not empty. This can be useful in order to conditionally create wrapper elements based on the given content.
@{ +main |
replace (
'/(.+)/is',
'<div class="wrapper">$1</div>'
)
}