Template:Replace/doc

From the Tesseract Wiki, the wiki for all things Marvel Cinematic Universe
Jump to navigation Jump to search
Information icon-grey.svg
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 function main in 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