GRAV CMS DEVELOPER

WhatsApp/Signal: +91-953-66666-77
Email ID: hello@gravdeveloper.com
How to use regex in Grav regex_replace() function in Twig template

How to use regex in Grav regex_replace() function in Twig template

  • Date: June 5, 2025
  • Tags: GRAV CMS

How to Use regex_replace() in Grav CMS Twig Template – 5 Practical Use Cases

When working with Grav CMS and Twig, formatting output content dynamically becomes essential for building clean and readable frontends. One of Twig's most powerful tools is the regex_replace() function. If you're looking to apply regular expressions in Grav using Twig, this guide will walk you through the process.

What is regex_replace() in Twig for Grav CMS?

The regex_replace() function in Twig is used to search and replace text based on a regular expression (regex). It helps you dynamically clean, format, or transform content in your templates without changing your actual content files.

Basic structure:

{{ variable|regex_replace('/pattern/', 'replacement') }}

Use Case 1: Remove Digits from a String

You might want to clean up a title or slug that includes unwanted numbers.

Example: Input: "BlogPost2025" Twig: {{ "BlogPost2025"|regex_replace('/\d+/', '') }} Output: BlogPost

Use Case 2: Strip HTML Tags from Content

If your field has raw HTML and you want to display plain text:

Input: "

This is bold

" Twig: {{ "

This is bold

"|regex_replace('/<[^>]+>/', '') }} Output: This is bold

Use Case 3: Replace Spaces with Dashes

For converting text into slugs or CSS class names:

Input: "Grav CMS Template" Twig: {{ "Grav CMS Template"|regex_replace('/\s+/', '-') }} Output: Grav-CMS-Template

Use Case 4: Remove Special Characters

To sanitize strings and keep only letters and numbers:

Input: "Hello@Grav#CMS!" Twig: {{ "Hello@Grav#CMS!"|regex_replace('/[^a-zA-Z0-9]/', '') }} Output: HelloGravCMS

Use Case 5: Format Phone Numbers

If you need to reformat a number string for display:

Input: "9876543210" Twig: {{ "9876543210"|regex_replace('/(\d{3})(\d{3})(\d{4})/', '($1) $2-$3') }} Output: (987) 654-3210

Important Tips

  • Always use forward slashes /pattern/ in your regex, and make sure to escape characters like \ properly.
  • Use single quotes in your regex pattern inside Twig to avoid syntax conflicts.
  • Test your regex patterns with sample text before applying them in your templates.
  • You can chain filters for more customization like |lower, |trim, or |capitalize.

Conclusion

The regex_replace() function in Twig templates for Grav CMS gives you powerful control over how content is presented. Whether you're formatting URLs, cleaning up input, or customizing display output, this feature helps you keep templates dynamic and clean. Mastering this filter will significantly improve your Grav theme development workflow.

For Grav CMS custom development or help with Twig templates, you can always reach out here: https://www.gravdeveloper.com/

Feel free to reach out if you'd like to make changes to your Grav website or simply chat about your Grav project. I'm available for a free consultation!