Questions tagged [replace]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
2
votes
1answer
120 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 ...
1
vote
2answers
38 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 ...
0
votes
1answer
62 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: ...
1
vote
1answer
78 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 |...
1
vote
1answer
554 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 ...
3
votes
1answer
3k 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 ...
2
votes
2answers
352 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 ...
7
votes
1answer
466 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 - ...
1
vote
1answer
417 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 ...
1
vote
0answers
37 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 ...
1
vote
2answers
327 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 ...
1
vote
1answer
167 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:...
1
vote
3answers
899 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 ...
3
votes
3answers
608 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*$/'...
1
vote
1answer
80 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/{...
0
votes
1answer
450 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 <...
3
votes
1answer
4k 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 ...
4
votes
2answers
3k 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 ...
6
votes
1answer
862 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'...
2
votes
1answer
3k 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 ...
3
votes
1answer
2k 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 ...
5
votes
1answer
18k views

How to replace characters in string

How do I replace ";" with "," in string "Red; Green; Blue"? {% set rgb = "Red; Green; Blue" %}