is there a way to escape an entire variable in Lua? like:
local blah = some_pattern
string.gsub(blah, "-", "%-")
The variable “blah” changes, and is used elsewhere in another pattern match. Whenever “blah” has a magic character in it, the second pattern match, that is done elsewhere, fails.
So I need to handle escaping the various magic characters at least, in “blah”.
Thanks in advance!
It’s a bit unclear what you’re asking for here: a way to call your escaping function whenever a variable’s value is set, or how to escape a string for pattern matching.
A variable is just storage for a value. You can’t cause a general operation to happen when a local variable is set. So if you change the value of
blah, it is up to you to escape the string properly again.If you’re asking how to escape a string for pattern matching, it would be something like this:
Where
...is the list of characters that you need to escape. The Lua reference manual has a list you can use. Note that, since this is a pattern, these characters must be escaped here.