Modulo:Replace

Da Wikizionario, il dizionario a contenuto aperto.

Questo modulo serve per sostituire determinate stringhe con altre decise arbitrariamente.

Sintassi

{{#invoke:Replace | replace_all | Parametro da considerare | parametro da sostituire | parametro sostituito }}

Esempio

{{#invoke:Replace | replace_all | test_1 | _ | - }}, sostituirà "test_1" con "test-1"


-- Module for different search and replace operations on strings.
local p = {}
 
-- Takes one string parameter, and returns the string with all characters with special meaning for Lua patterns escaped with a preceding `%`.
function p.escape_pattern(text)
    -- Replaces each occurence of any of ().%+-*?[^$ with a `%` and then the character.
    local r = string.gsub(text, "[%(%)%.%%%+%-%*%?%[%^%$]", "%%%1")
    return r
end
 
-- Returns the first parameter, with all occurences of the second parameter replaced with the third parameter.
-- All special characters are ignored: {{#invoke:StringReplace|replace_all|test.a%1$foo|%1|bar}} results in `test.abarfoo`.
function p.replace_all(frame)
    local str = frame.args[1]
    local strToFind = frame.args[2]
    local strToreplaceWith = frame.args[3]
    local r = string.gsub(str, p.escape_pattern(strToFind), p.escape_pattern(strToreplaceWith))
    return r
end
return p