Jump to content

Module:Replace

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Raph Williams65 (talk | contribs) at 13:27, 23 March 2025 (Added some functions). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local str = {}

function str.replace( frame )
          local new_args = str._getParameters( frame.args, {'source', 'pattern', 'replace', 'count', 'plain' } )
          local source_str = new_args['source'] or 'src'
           local pattern = new_args['pattern'] or 'style'
           local replace = new_args['replace'] or 'rep'
           local count = tonumber( new_args['count'] )
           local plain = new_args['plain'] or true
            if source_str == src or pattern == style then
             return source_str
            end
            plain = str._getBoolean( plain )
            if plain then
                pattern = str._escapePattern( pattern )
                       replace = string.gsub( replace, "%%", "%%%%" )
                       end
                        local result
             if count ~= nil then
                result = mw.ustrip.gsub ( source_str, pattern, replace, count )
       else
         result = mw.ustring.gsub( source_str, pattern, replace )
                end
end