Skip to content

Instantly share code, notes, and snippets.

@TaizWeb

TaizWeb/escape-fix.lua

Last active Apr 27, 2020
Embed
What would you like to do?
A function to fix escape sequences in lua strings. Useful for preparing JSON from an API for conversion.
function escapeFix(data)
local newData = ""
for i=1,string.len(data) do
local char = string.sub(data, i, i)
local nextChar = string.sub(data, i+1, i+1)
local prevChar = string.sub(data, i-1, i-1)
if (char == '"' and (prevChar ~= "{" and nextChar ~= ":" and prevChar ~= ":" and nextChar ~= "," and prevChar ~= "," and nextChar ~= "}")) then
newData = newData .. "\\" -- Append another, non-escaped, '\'
end
newData = newData .. string.sub(data, i, i)
end
newData = string.gsub(newData, "\n", "\\n") -- Strip out newlines as well
return newData
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment