Not a developer? Go to MovableType.com

Documentation

regex_replace

Applies a regular expression operation on the input. This filter requires two values: the first is the pattern and the second is the replacement. Learning regular expressions is outside the scope of this document, though it is likely helpful to know that Perl-style regular expressions are supported. RegExr is a tool for learning, testing, and sharing regular expressions; a desktop version also available.

Note that if you don’t need regular expression support, the replace modifier is simpler and faster.

Examples

Strip a Bracketed Phrase

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

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

Use Back References

Back references can be used in the replacement, such as this example that prints the first three words (if available, but at least 1) in the Entry Title:

<mt:EntryTitle regex_replace="/^(\w{1,3}).*/","$1">

Modifiers

Regex modifiers are supported. The global modifier, for example, will find all occurrences of bracketed phrases and remove them:

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

Or, a case-insensitive search to strip the word “apple” from text (in other words, it would find “Apple” or “aPpLlE”):

<mt:EntryTitle regex_replace="/\s*apple\s*/i","fruit">

Capturing Text in Parentheses

If blog description is:

<strong>I am the walrus</strong> <em>(Koo Koo Ca Choo)</em>

Use this regex_replace to get the part in parenthesis:

<mt:BlogDescription regex_replace="/(.*)<em>\((.*)\)</em>(.*)/","<em>$2</em>">

Output will be:

<em>Koo Koo Ca Choo</em>

Replace Modifier

As noted above, the replace modifier is a better choice when a simple text search and replace is needed. An example of an operation better handled by replace is stripping the “.html” extension from URLs. Simply, a regular expression is not needed to do this type of search:

<mt:EntryPermalink replace=".html","">
Back

Leave a Comment