Skip to main content
Share Your Experience: Take the 2024 Developer Survey

Questions tagged [regex]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
1 vote
1 answer
101 views

Syntax for URL route instead of using url parameter

I'm confused by the syntax for URL routes. Using Craft 3. I'm working with two separate category groups and have urls like this: site.com/?parameter=123 site.com/abc?parameter=123 site.com/abc/xyz?...
Jake's user avatar
  • 43
1 vote
1 answer
193 views

Best way to do a multi-line regex replace?

I understand that for a long time the |replace filter has supported regex modifiers, but I can't seem to get the syntax right for multiline searches. For example, here I want to extract the internal ...
Jonathan Schofield's user avatar
1 vote
1 answer
680 views

Regular Expression replace not working within Twig Template

I'm attempting to capture 4 attributes of an Attributes of an tag and injecting inline styles to the style attribute. Craft CMS and Twig templates are being used to generate HTML emails here, hence ...
Christopher DuCharme's user avatar
2 votes
1 answer
163 views

Fixing a YouTube regex in a replace filter

I need to take the dominant YouTube URL schemas and convert them into a YouTube embed URL of the form: https://www.youtube.com/embed/VIDEOID I know there are technically loads of possible YouTube URL ...
Jonathan Schofield's user avatar
0 votes
1 answer
149 views

Retour Regex to insert a new directory

I'm building out a new site and have moved all the existing client asset upload directories into one parent directory. old site: /hero/ /books/ /slider/ new site: /uploads/hero/ /uploads/books/ ...
CreateSean's user avatar
  • 1,973
1 vote
1 answer
417 views

RegEx Redirects for Legacy URLs

My legacy URLs have this structure: /teachings/tag/13721/omnipotent/ or /teachings/series/17764/fearless/ For both the tag and the series they are always followed by a string of digits and then slug ...
Jonathan Simcoe's user avatar
1 vote
1 answer
103 views

Default Users Password Regex

Assume a front end registration form. I would like to validate user passwords before posting to the server. I have not been able to find the default password requirements as of yet for craft 3. Could ...
Seraphim Jester's user avatar
1 vote
1 answer
231 views

Replace throwing Unknown Modifier Error

I've tested the regex and seems to work perfectly, but when placing it within Craft it seems to throw the following error: preg_replace(): Unknown modifier '/' {% set ig = user.instagram|raw|...
JMKelley's user avatar
  • 1,406
1 vote
1 answer
104 views

RexEg Pattern to Exclude Using the Retour Plugin

The Retour plugin has this setting: Exclude Patterns - Regular expressions to match URLs that should be excluded from Retour. How would I exclude images (jpg and png) from being handled by Retour? My ...
Stephen's user avatar
  • 685
1 vote
1 answer
554 views

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

I'm trying to route users to a "locations" structure entry that normally has a URL structure that looks like: locations/<slug> My client wants to use the structure of links from their old site ...
slander's user avatar
  • 73
1 vote
1 answer
146 views

Craft 3 RegEx problem

I am trying to change all rgb colours in a bit of text/content. I have the RegEx working here in php using preg_replace: preg_replace("/#[a-zA-Z0-9]{6}|rgb\((?:\s*\d+\s*,){2}\s*[\d]+\)|rgba\((\s*\d+\...
Green17's user avatar
  • 181
1 vote
1 answer
381 views

striptags but leave link test in between

I have a rich text (redactor field) that I'm using striptags on to remove the html markup from the front end. However I noticed for links it whips out the link and the text in between. Is this ...
Danielle Frappier's user avatar
1 vote
1 answer
128 views

|replace filter is matching too many characters

I'm looking to format out image tags from a string using the |replace filter. The behavior I'm looking for is to give it a string like this: <img src="test.jpg"><p>some text</p><...
jtenclay's user avatar
  • 311
3 votes
1 answer
6k views

Twig replace regex

As I understand, craft overrides Twig's replace filter to allow for the use of regex; which is just running preg_replace under the hood When running the following regex through preg_replace in a php ...
jerle's user avatar
  • 33
2 votes
2 answers
536 views

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

I’m trying to insert a programmatically generated id attribute into child elements of parsedown processed text. I'm aware that Craft’s twig replace filter incorporates preg_replace, so… I have a ...
Jonathan Schofield's user avatar
7 votes
1 answer
807 views

Does Craft / Twig support regex groups?

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 - ...
haabe's user avatar
  • 113
1 vote
1 answer
181 views

Possible to use REGEX Backreference in Routes

I've searched and searched for info on regex backreferences in Craft's route.php file to no avail. I have a URL structure required by the client along the lines of: / [state] / [section] / region / ...
Shane Robinson's user avatar
1 vote
1 answer
117 views

Using 'replace' with a regex is throwing an error. Any ideas why?

I am attempting to strip out <p> tags around <img> tags that are automatically getting generated in a WYSIWYG field. The error: preg_replace(): Unknown modifier '&gt;' {{ story.html|...
Wedodan's user avatar
  • 111
1 vote
1 answer
175 views

Element API - UrlHelper generates backwards slashes (regex)

I am generating a url within Element API: 'profile' => UrlHelper::getUrl("directory/company/{$member->id}/{$urlslug}"), which outputs: http:\/\/craft.fira\/directory\/company\/1501\/abet-...
Adam Menczykowski's user avatar
3 votes
1 answer
2k views

How do I create an advanced route with multiple slugs?

I'm trying to create a route for an RSS feed which allows for the following routes. domain/feed domain/category/feed domain/category/subcategory/feed I have the first two routes working and the ...
Seán O'Grady's user avatar
4 votes
2 answers
4k views

Regex to remove trailing slash not working

I am trying to remove the http:// and trailing slash from a string, so my replace filter looks like this: website|replace({ 'http://': '', '/\\/$/': '' }) The http:// part works, the trailing slash ...
Katrin's user avatar
  • 674
2 votes
1 answer
484 views

Route that should be matching numbers not matching a zero?

I have the following in my routes.php file: return array( 'articles/(?P<sectionId>\d+)/(?P<offset>\d+)' => 'articles/index' ); And while localhost/articles/4/1, for example, ...
Patrick Harrington's user avatar
1 vote
1 answer
248 views

How to redirect a url with a query to one that does not have a query

I'm using Redirect Manager (https://github.com/rkingon/Craft-Plugin--Redirect-Manager) and I"m trying to redirect: www.website.com/?post_type=testimonials To: www.website.com/about-us I've tried the ...
Deceptikong's user avatar
2 votes
1 answer
4k views

Use regex in replace filter

any idea why this: %20AND%20foo%3A%22(.+?)%22 won't match this: %20AND%20foo%3A%22some random string of characters%22 in the Twig |replace filter? If i do this: {% set foo = '%20AND%20foo%3A%22some ...
Florian's user avatar
  • 428
1 vote
2 answers
219 views

Workaround for deprecated regex pattern modifier in PHP 5.5.0 and up (10.10 Yosemite)

I'm relying on the Low Regex plugin to convert section handles from camelCase to lower-dash-case to use them in URLs, like so: {section.handle|regex('/(^|[a-z])([A-Z])/e', 'strtolower(strlen("\\1") ? ...
rppld's user avatar
  • 336
5 votes
1 answer
3k views

Double escaping Regular Expressions in Craft's Twig replace filter?

Just trying to use Craft's Twig replace filter to remove number prefixes from the beginning of User Groups names and I have had to double escape the character classes etc to get it to work. I tried ...
Neal Hulme's user avatar
4 votes
2 answers
794 views

Can I use Regex in the Find and Replace tool?

Does the Find and Replace tool support regex? I need to remove a certain section of an image url for all images in the 'field_body' column. So if I had an image referenced as such http://craft.dev/...
Seán O'Grady's user avatar
15 votes
3 answers
59k views

How can I perform a RegEx match in Twig?

Is it possible to perform a RegEx match on a string in Twig? I'm looking for something like this: {% set string = "This is **string**. **wer** qewr **234**" %} {% set match = string.matches(/([*][*...
nicael's user avatar
  • 2,382