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 this...

{{ group.name|replace('/^\d+\.\s/','') }}{% if not loop.last %}, {% endif %}


and it didn't work so for some reason I tried this...

{{ group.name|replace('/^\\d+\\.\\s/','') }}{% if not loop.last %}, {% endif %}


...and it worked. Is there a sensible explanation for this as Regular Expressions are like Voodoo?!
Twig auto-escapes the \ character so by the time it makes it to the Craft replace filter PHP code, the regex string looks like /^d+.s/.

By double escaping it, you're forcing the correct regex to make it through /^\d+\.\s/.