Jump to content

Topic on Help talk:Extension:ParserFunctions

#replace multiple strings?

3
Summary last edited by Tacsipacsi 12:48, 17 June 2023 1 year ago

Use nested #replaces.

V G5001 (talkcontribs)

Is it possible to replace multiple different strings within one string?

For example, I would want to do {{#replace:The dog is jumping.|dog,jumping|cat,walking}} or something similar to receive the output ”The cat is walking.”

Is this possible in any way?

Dinoguy1000 (talkcontribs)

Yes, just nest #replaces: {{#replace:{{#replace:The dog is jumping.|dog|cat}}|jumping|walking}}

Just be aware of the expansion depth limit (to say nothing of code readability); if you need a lot of separate replaces on the same string, it will probably be better to write it in Lua, as a Scribunto module. (You could also use Extension:Variables, but that extension unfortunately has an uncertain future given the direction the parser is headed in.)

V G5001 (talkcontribs)

Thanks, this worked