Jump to content

Template:Replace

From SiIvaGunner Wiki
Template documentation
Note: portions of the template sample may not be visible without values provided.
View or edit this documentation. (About template documentation)
Editors can experiment in this template's sandbox and test case pages.

This template replaces occurrences of any number of search terms in a string. By default, all occurrences of the search terms will be replaced with the desired terms, but particular occurrences of a search term (e.g., the first occurrence) can be specified.

This template uses Lua's Regular Expression (RegEx) version, which can also be used in this template's parameters. See here for an explanation on how to use this RegEx variant.

Syntax

Base function

{{Replace|<input string>|<search term>|<replacement term>}}

Multiple replacement

{{Replace|<input string>|<search-1>|<replace-1>|<search-2>|<replace-2>|<...>|<search-X>|<replace-X>}}

  • Each pair of unnamed parameters after the input string will be treated as a pair of search and replacement terms
Direct removals

{{Replace|<input string>|s1 = <search-1>|s2 = <search-2>|s3 = <search-3>}}

  • When no pairing replacement term is given, the searched term will instead be removed from the input string.
Mixed usage

{{Replace|<input string>|s1 = <search-1>|s2 = <search-2>|r2 = <replace-2>|s3 = <search-3>|r4 = <replace-4>}}

  • search-1 will be removed
  • search-2 will be replaced by replace-2
  • search-3 will be removed
  • As no search-4 was given, replace-4 is ignored
Key terms

The following terms will be replaced by the specified characters when used in the search or replace parameters

  • {space} by  
  • {newline} by <br>

Examples

{{Replace|<hi>|<i>|<ello>}}
{{Replace|<hello there>|<hello t>|<I am{space}>}}
{{Replace|<spaces are removed>|<{space}>|<...>}}
{{Replace|<01010101010>|<0>|<ZERO>|<1>|<one>}}
{{Replace|<01010101010>|<0>|<ZERO>|<1>|<one>}}
{{Replace|<01010101010>|<0>|<ZERO>|<1>|<one>}}
{{Replace|<this supports regular expressions too>|<s(.-)s>|<B%1E>}}

Template Data

Replaces all occurrences of search term in a string with the replacement term.

Template parameters

This template prefers inline formatting of parameters.

ParameterDescriptionTypeStatus
Input String1

no description

Stringrequired
Search Term 12 s1

no description

Stringrequired
Replace Term 13 r1

no description

Stringoptional
Search Term 2s2

no description

Stringoptional
Replace Term 2r2

no description

Stringoptional
Search Term 3s3

no description

Stringoptional
Replace Term 3r3

no description

Stringoptional
Search Term 4s4

no description

Stringoptional
Replace Term 4r4

no description

Stringoptional
Search Term 5s5

no description

Stringoptional
Replace Term 5r5

no description

Stringoptional

Debug data: