Template:Replace/doc
Jump to navigation
Jump to search
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))
- Escaped parentheses should work properly inside grouping (e.g.
$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
- Expected:
{{subst:replace|Abyssal whip detail.png|detail\.png}}→Abyssal whip- Expected:
Abyssal whip
- Expected:
{{replace|abcde|(ab¦c¦d)|^$1^}}→^ab^^c^^d^e- Expected:
^ab^^c^^d^e
- Expected:
{{replace|abcde|(a¦b¦d)|$$1###-}}→$a###-$b###-c$d###-e- Expected:
$a###-$b###-c$d###-e
- Expected:
{{replace|abcde|(ab¦a¦b)|!$1!}}→!ab!cde- Expected:
!ab!cde
- Expected:
