i am looking to write a function in Lua that replaces all occurrences of one string with another, e.g.:
function string.replace(s, oldValue, newValue)
return string.gsub(s, oldValue, newValue);
end;
What i need (unless Lua already has a string replace function) is a function to escape Lua regular expressionpattern strings (unless Lua already has an EscapeRegularExpressionPattern function)
i've tried to start writing the proper string.replace function:
local function EscapeRegularExpression(pattern)
-- "." ==> "%."
local s = string.gsub(pattern, "%." "%%%.");
return s;
end;
function string.replace(s, oldValue, newValue)
oldValue = EscapeRegularExpression(oldValue);
newValue = EscapeRegularExpression(newValue);
return string.gsub(s, oldValue, newValue);
end;
But i cannot easily think of all the Lua regular expressionpattern keywords that need to be escaped.
Bonus Example
Another example that needs a fix might be:
//Remove any locale thousands separator:
s = string.gsub(s, Locale.Thousand, "");
//Replace any locale decimal marks with a period
s = string.gsub(s, Locale.Decimal, "%.");
IMG). And i think in html therantelement is forbidden from having an opening tag.