Information icon-grey
This is a documentation subpage for Template:Replace.
It contains usage information, categories, and other content that is not part of the original template page.
Template:Replace invokes Module:Replace using Lua.

Acts as a regex-supporting version of {{#replace:}}.

Some functions of standard regex (Perl compatible) are not supported.

  • No flags (g, i, m)
  • No alteration (|); see below
  • No generalized quantifier ({m,n})
  • No word boundaries (\b)

Lua's pattern matching is used for this template; however, the following replacements are made to give some functionality to standard regex:

  • *?-
  • \%
  • $1%1 (supported for all numbers 1–9)

Any whitespace in the output will be automatically trimmed and condensed. Any HTML entities (such as ' from {{subst:PAGENAME}}) will be automatically converted to their actual character.


If detected, the module can emulate very simple alteration patterns within groups, with the following limitations:

  • No supported for nested grouping (e.g. (foo|bar(and|or)baz)
    • Escaped parentheses should work properly inside grouping (e.g. (foo|bar\(or\)baz))
  • $ will cause unwanted behaviour inside any groups, even if that group doesn't use alteration
    • This only applies to patterns where at least 1 group has an alteration. If all groups are single captures, this behaviour won't occur.
  • The pipe character breaks wikitext. Use {{!}} or ¦ inside patterns
MediaWiki docs

For full information on the difference between Lua's pattern matching and regex, see Lua reference manual § Patterns.


This template should always be substituted.

{{subst:replace|text to perform on|search pattern|replacement}}
Expected: capture
{{subst:replace|Abyssal whip detail.png|detail\.png}}Abyssal whip
Expected: Abyssal whip
Expected: ^ab^^c^^d^e
Expected: $a###-$b###-c$d###-e
Expected: !ab!cde

Community content is available under CC-BY-SA unless otherwise noted.