Skip to main content
7 votes
Accepted

Does Craft / Twig support regex groups?

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, ...
Brandon Kelly's user avatar
5 votes

How can I perform a RegEx match in Twig?

This works for me just fine: {# Removes all characters other than numbers and + #} {{ profile.phone|replace('/[^0-9+]/', '') }}
medoingthings's user avatar
4 votes
Accepted

Twig replace regex

Try doubling-up your backslashes: https://craftcms.com/docs/templating/filters#replace
Jordan Lev's user avatar
3 votes

RegEx Redirects for Legacy URLs

Here's the regex I think you'll need: \/teachings\/tag\/\d+\/(\w+)\/? → \/teachings\/topics\/$1 \/teachings\/series\/\d+\/(\w+)\/? → \/teachings\/series\/$1 A couple explanations: \/ is an escaped /...
Jalen Davenport's user avatar
  • 3,045
3 votes
Accepted

Syntax for URL route instead of using url parameter

Using routes You can define a route like this in your config/routes.php: return [ '<country:{slug}>/<city:{slug}>/<productType:{slug}>' => ['template' => '_category'], ]; ...
MoritzLost's user avatar
  • 11.6k
3 votes
Accepted

Routing: How can I ignore a parameter in route definition?

It sounds like you might be trying to blend Craft/Yii routing with Rewrites (if you're coming from an Apache background). I believe what Craft interprets this as is an attempt to map a request to ...
August Miller's user avatar
2 votes

Fixing a YouTube regex in a replace filter

Doh! It was my ternary logic that was failing not the regex. I’m now doing this: {% set vidUrl = block.videoUrl %} {% if 'youtu' in vidUrl %} {# Check for an 'embed' URL which may need cleaning #}...
Jonathan Schofield's user avatar
2 votes
Accepted

Craft 3 RegEx problem

You were close! Just don't use the Twig hash/array syntax when using the |replace filter if you want Craft to process it as regex. This should do what you're looking for: {% set svg_image_output = ...
Brad Bell's user avatar
  • 67.5k
2 votes
Accepted

RexEg Pattern to Exclude Using the Retour Plugin

The RegEx is only the URI part; so something like (.*.png|.*.jpg) is probably what you want.
andrew.welch's user avatar
  • 11.6k
1 vote

Best way to do a multi-line regex replace?

I'm on my phone, so I'm not able to test, but it looks to me like your regex is explicitly expecting the string you're testing to start with <ul> and end with </ul>, and despite the ...
stevecomrie's user avatar
1 vote

Regular Expression replace not working within Twig Template

You could try to use Retcon for that: https://github.com/mmikkel/Retcon-Craft/blob/master/README.md
Max Strebel's user avatar
1 vote

Default Users Password Regex

There are a couple of existing plugins that look like they might do what you're looking for: https://plugins.craftcms.com/password-policy https://plugins.craftcms.com/enforce-password And of course,...
Brad Bell's user avatar
  • 67.5k
1 vote
Accepted

Replace throwing Unknown Modifier Error

You'll need to double escape those backslashes in the regex. {% set ig = user.instagram|raw|replace('/(?:(?:http|https):\\/\\/)?(?:www.)?(?:instagram.com|instagr.am)\\/([A-Za-z0-9-_\\.]+)/im') %}
Brad Bell's user avatar
  • 67.5k
1 vote

Using a back reference inside a regex search string within replace filter

A workaround in light of my comments on Brandon’s reply… If I ignore the attempt to act on child tags only, I can search for opening tags of all generated elements (regardless of whether they are ...
Jonathan Schofield's user avatar
1 vote

Using a back reference inside a regex search string within replace filter

Backreferences within the regex are normally defined as \1, \2, etc. In the |replace filter you need to escape all backslashes, so it’ll be \\1, \\2, etc.
Brandon Kelly's user avatar
1 vote

striptags but leave link test in between

Actually, excluding certain tags from being removed is built into the striptags filter. You can use it like this: {{ articleTextTrim | striptags('<a>') }}
Jalen Davenport's user avatar
  • 3,045
1 vote
Accepted

|replace filter is matching too many characters

Something like this should work: {{ snippet|replace('/<img([\w\W]+?)>/', '') }} You can see an explanation of the regex in the right column of https://regex101.com/r/DvJHeY/1
Brad Bell's user avatar
  • 67.5k
1 vote
Accepted

Possible to use REGEX Backreference in Routes

Is it simply not possible to use backrefs in the target route templates Right now it's not possible (although might be worth a feature request). The right hand side of the route key => value pair ...
Brad Bell's user avatar
  • 67.5k
1 vote

Can I use Regex in the Find and Replace tool?

I'll add my two cents here, since it appears you are removing Wordpress created images (which I also did). I wrote a regex initially to find all these duplicated files (this command works ...
hisnameisjimmy's user avatar
1 vote

How can I perform a RegEx match in Twig?

Answers for 2023 using Craft CMS 4 For simple find and replace: you can just use the replace filter, it accepts RegEx now. For more complicated lookups: If you're looking to just add Craft CMS filters ...
franklylately's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible