regex_replace

Applies a regular expression operation on the input. This filter accepts two input values: one is the pattern, the second is the replacement.

Example:

    <$mt:EntryTitle regex_replace="/\s*\[.+?\]\s*$/",""$>

This would strip any bracketed phrase from the end of the entry title field.

This page was last updated on 2008-02-23, 13:22.

10 Notes

Back-references:

Use $1, $2, $3, etc., in the replace field to echo the corresponding matched phrases within parentheses in the search field.

Note that the "g" global modifier is valid here, which will find all occurrences, not just one. Extending the above example:

List entry categories if their label is within the set Explore, Savor, Inside, Offbeat, or Celebrate and then glue them with ", ". (The regex_replace modifiers remove extra glue from beginning, end, and between categories that don't match the if statement)

        ">

This is only here as an example, a much better solution would be to use mt:SevVarBlock with function="push" attribute and then output with mt:Loop

Search for case-insensitive "the" with optional spaces on either side and replace with nothing. Then dirify the result.

Thus if the blog name was "The Big Lebowski", it would be converted to "big-lebowski".

If blog description is:

I am the walrus (Koo Koo Ca Choo)

Use this regex_replace to get the part in parenthesis:

\((.*)\)(.*)/","$2"$>

Output will be:

Koo Koo Ca Choo

Remove the "http://www" from a url:

">

RegExr is a tool for learning, testing, sharing regular expressions. (desktop version also available)

Remove the ".html" from the entry permalink:

Hi Beau,

I'm trying to use the container tag to create a list of parent categories such as: Food/Italian/Pizza/Pepperoni and then apply a regexreplace to pull the name of the 2nd Category. So in that list, the regexreplace would return "Italian". I've tried so many variations and it's starting to do my head in. Can you recommend a solution to pull that 2nd category name with this method? I just can't seem to get it working.

If you want to replace the "-" with "_" in your category label:

Leave a note

Have a question? Please use the MT Forums. Notes submitted on documentation should pertain to tips & hints regarding documentation. Your note may be removed once its contents have been integrated into the body of the page.