FANDOM


Documentation icon Template documentation[edit] [purge]
This documentation is transcluded from Template:Replace/doc.
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.

Alteration

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.

Usage

This template should always be substituted.

{{subst:replace|text to perform on|search pattern|replacement}}
Examples
{{subst:replace|textcapturetext|text(.*)text|$1}}capture
Expected: capture
{{subst:replace|Abyssal whip detail.png|detail\.png}}Abyssal whip
Expected: Abyssal whip
{{replace|abcde|(ab¦c¦d)|^$1^}}^ab^^c^^d^e
Expected: ^ab^^c^^d^e
{{replace|abcde|(a¦b¦d)|$$1###-}}$a###-$b###-c$d###-e
Expected: $a###-$b###-c$d###-e
{{replace|abcde|(ab¦a¦b)|!$1!}}!ab!cde
Expected: !ab!cde
Community content is available under CC-BY-SA unless otherwise noted.