7

I see Craft does support regex in replace search. But there is no info on using regex groups, ie string | replace('/this is a (string)/', 'This is another $1').

Is this feature implemented? If so - how?

1
  • Why can't I use the word boundaries? /bWord/b is also finding
    – Gudehus
    Commented Aug 15, 2020 at 12:21

1 Answer 1

7

Yes.

{{ 'this is a string'|replace('/this is a (string)/', 'This is another $1') }}

will output

This is another string

When you pass something that looks like a regex into the |replace filter, preg_replace() is called behind the scenes, so |replace can do anything preg_replace() can do.

1
  • Ah, thanks. I must somehow have had some typo in my code, as Craft threw an error on $1 when I tried initially.
    – haabe
    Commented Mar 7, 2017 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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