TransWikia.com

Does Craft / Twig support regex groups?

Craft CMS Asked by haabe on August 31, 2020

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?

One Answer

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.

Correct answer by Brandon Kelly on August 31, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP