Skip to main content

Questions tagged [replace]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
1 vote
1 answer
208 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
170 views

Replace HTML string in body tag?

I have thousands of imported entries which have HTML image embeds to files which no longer exist. Is there a way to use the replace function in the tag to say replace <img src="[variable ...
Emma Swann's user avatar
2 votes
1 answer
165 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
1 vote
1 answer
42 views

Replace # with entry.title

Good morning and thank you in advance. How can I replace '#' with the entry.title. I have tried everything and I keep getting hash errors. Current code is this: <h3>{{ wordBoldTitle|replace({'#'...
Jason's user avatar
  • 107
2 votes
1 answer
470 views

Replace substring in string with variable named as substring?

Could you help me with something? In a frontend template, I'm trying to go through the string, find all substrings between double curly brackets {{ something }} using regular expressions and replace ...
Michal Bulla's user avatar
1 vote
2 answers
541 views

Replace in the first instance?

I am trying to insert a div that'll call an ad between two paragraphs of copy text. However, I'd like to do it only one instance. The current replace code I'm using is this - which is inserting my ...
Emma Swann's user avatar
0 votes
1 answer
184 views

How can I add and replace classes in the markdown field?

Prompt please both in the field {{ block.markdownContent|markdown|raw }} replace and add classes? For example: <p> replace by: <p class="section__paragraph text"> <h2> replace by: ...
paveell's user avatar
  • 31
1 vote
1 answer
158 views

Can I call a filter on all Rich Text fields?

I'm looking to dynamically replace the ℠ glyph with <sup>SM</sup> in all rich text fields, since that glyph doesn't exist in the font we're using. I'd like to avoid having to call the |...
jtenclay's user avatar
  • 311
1 vote
1 answer
2k views

Remove Quotes from Text Fields

My client needs to input apostrophes in text fields for staff names, such as ’ or just the plain ' and as you can imagine, it is causing issues. I have tried this replace({'\'',''}) and this ...
isral's user avatar
  • 113
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
810 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
745 views

How to replace a html entity within script tags using Craft replace

Apologies if this has been asked before, I can only find questions dealing with the reverse scenario (i.e. replacing special characters with html entities) I need to output special characters as part ...
S. Connolly22's user avatar
1 vote
0 answers
61 views

Listen to replacing files through CP's assets section

Admin users can replace asset files without having to deal with re-uploading/re-creating a new asset using the feature/technique described in Peter's answer here: Is it possible to replace uploaded ...
yoannisj's user avatar
1 vote
2 answers
509 views

Automatically link to entries in rich text field

Has anyone implemented auto linking of words within a redactor field? For example, a rich text field would contain this: ​A small boat surrounds a large ​island​​ In a channel there are entries ...
Leah's user avatar
  • 441
1 vote
1 answer
181 views

Filter rules as a variables

I want to strip every headline tag from all Rich Text fields site-wide. So I thought about adding a replace filter. But since this will be site wide I wanted to write the replace rules in a variable:...
KSPR's user avatar
  • 3,786
1 vote
3 answers
1k views

Replace placeholder string in text field with HTML generated by Asset query

I have a Matrix block with a text field and an Assets field. The text field has a Markdown filter applied to it. I'd like the content owner to be able to enter a placeholder reference to one of the ...
Jonathan Schofield's user avatar
3 votes
3 answers
1k views

Trim trailing spaces before each new line in a multi-line string

I have a string from a multi-line plain text field and I'd like to trim any trailing spaces before new lines. With plain regex I would use this expression: /\s*$/gm Using {{ string|replace('/\\s*$/'...
Katrin's user avatar
  • 674
1 vote
1 answer
87 views

Is it possible to create a cleaner replace?

any help on this one appreciated. I have an entry url that is automated to take several fields and make the the uri. It pulls the jobTitle, jobCity and jobId fields and then... well... this: jobs/{...
Pathfinder's user avatar
1 vote
1 answer
679 views

Can I convert rich text field to have plain text output - with carriage returns?

I am exporting some fields to an XML feed. There is a rich text field with typically 4 paragraphs in it. The default export kicks out: <p>Some text</p> tags as well as stuff like <...
Pathfinder's user avatar
3 votes
1 answer
6k views

Get a twig | replace to output html rather than the text

I've got a craft field where I have an such as: Learn more about this great %Job Title% job. I'm then using Twig Replace to insert the actual entry.jobTitle into the %Job Title% space. This works ...
Pathfinder'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
6 votes
1 answer
1k views

Replace <p> with <p class="myClass">

Currently I am using twig's replace filter to replace the tags: {% set str = block.body | raw %} {{ str | replace('<p>', '<p class="myClass" data-wow-duration="1s">') }} But on output it'...
Leah's user avatar
  • 441
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
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
7 votes
1 answer
24k views

How to replace characters in string

How do I replace ";" with "," in string "Red; Green; Blue"? {% set rgb = "Red; Green; Blue" %}
nicael's user avatar
  • 2,382