3

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 page (outside of craft) in order to match non-ASCII characters; it works as expected:

/([^ !"#$%&\'\(\)*\+,-\.\/0123456789:;<=>\?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\[\]\^\\\_`abcdefghijklmnopqrstuvwxyz{\|}~\n]+)/u

However, when running it through replace in a template in craft; nothing is matched

Any ideas as to why this is happening and possible solutions?

Thanks

1 Answer 1

4

Try doubling-up your backslashes:

https://craftcms.com/docs/templating/filters#replace

2
  • Good shout, that fixed it. Any idea why double back-slashes are required?
    – jerle
    Commented Apr 18, 2017 at 8:47
  • I don't know exactly for sure, but in general whenever you're passing one language through to another (in this case, passing a regex through twig), you need to double-escape if both languages share the same escape characters. Because the first language (twig in this case) will escape the backslash, but you want an actual backslash passed through to the regex (so it can then escape the character after it), so you need to tell twig "I want an actual backslash here to be sent to the regex". Or something like that.
    – Jordan Lev
    Commented Apr 18, 2017 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.