Skip to main content

before_regex

Returns a string which is the subset of haystack before the match to the regular expression pattern and not including the matched pattern itself.

If no matches to pattern are present in `haystack`, returns an empty string.

If pattern is empty or is an invalid regular expression, throws an exception.

Usage

{{before_regex text pattern flags="t"}}

Arguments

haystack
The input string.

pattern
A valid regular expression.

flags

  • t: trim the final result prior to returning (remove all whitespace from beginning and end)

Examples

Return text before first match of pattern (assuming email.body contains "This is red and that is blue and that is green.")

{{before_regex email.body '/that/' flags='t'}}
This is red and

Return text before last match of pattern (assuming email.body contains "This is red and that is blue and that is green.")

{{before_regex email.body '/that/' flags='tr'}}
This is red and that is blue and

Return an empty string if no match to pattern

{{before_regex email.body '/pizza/' flags='t'}}