Module:Pattern utilities

From The Languages of David J. Peterson
Revision as of 13:09, 3 September 2023 by Djpwikiadmin (talk | contribs) (Created page with "local export = {} -- A helper function to escape magic characters in a string when interpolating a string into a Lua pattern. -- Magic characters: ^$()%.[]*+-? function expor...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

No docs in Wiktionary


local export = {}

-- A helper function to escape magic characters in a string when interpolating a string into a Lua pattern.
-- Magic characters: ^$()%.[]*+-?
function export.pattern_escape(text)
	if type(text) == "table" then
		text = text.args[1]
	end
	text = mw.ustring.gsub(text, "([%^$()%%.%[%]*+%-?])", "%%%1")
	return text
end

-- A helper function to escape magic characters in a string when interpolating a string into a Lua pattern replacement
-- string (the right side of a gsub() pattern substitution call).
-- Magic characters: %
function export.replacement_escape(text)
	if type(text) == "table" then
		text = text.args[1]
	end
	text = mw.ustring.gsub(text, "%%", "%%%%")
	return text
end

return export