Search code examples
stringluamatch

How'd i loop through every string.gmatch in a string then replacing the matches with a bytecoded version of that?


How'd I change everything that it matches in the string without changing the non matches?

local a = "\" Hello World! I want to replace this with a bytecoded version of this!\" but not this!"

for i in string.gmatch(a, "\".*\"") do
    print(i)
end

For example I want [["Hello World!" Don't Replace this!]] to [["\72\101\108\108\111\32\87\111\114\108\100\33" Don't Replace this!]]


Solution

  • local a = "\"Hello World!\" but not this!"
    
    print(a:gsub('"[^"]*"', function(str)
      return str:gsub('[^"]', function(char)
        return "\\" .. char:byte()
      end)
    end))