Using replace filter we can match pattern using regular expressions, but can we use part of the matched string in the second argument of filter? For example, I want to match all numbers and replace each matched number with a matched number enclosed in parentheses.

1 Answer 1


Craft's replace filter will just use PHP's preg_replace method behind the scenes if it detects that a regular expression has been passed in.

It sounds like you're looking for the PHP preg_match method, which isn't available natively.

There was a plugin that added support for this in Craft 2, but I don't think it's been ported to Craft 3. You might be able to use that as a starting point if you'd like to port it, though.

  • 1
    I just created a Craft 3 version of this plugin with a few more filters. Still in testing before submission to plugin store but can be installed via composer command. github.com/indigoviking/expressive Dec 28, 2018 at 23:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.