我想在我的Lua(Luvit)项目中使用decodeURI或decodeURIComponent,就像在 JavaScript中一样. JavaScript的: decodeURI('%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82')// result: привет 很喜欢: require('querystring').urldecode('%
JavaScript的:
decodeURI('%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82') // result: привет
很喜欢:
require('querystring').urldecode('%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82') -- result: '%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82'如果您理解 URI percent-encoded format,那么在Lua中做这件事是微不足道的.每个%XX子字符串表示用%前缀和十六进制八位字节编码的UTF-8数据.
local decodeURI do local char, gsub, tonumber = string.char, string.gsub, tonumber local function _(hex) return char(tonumber(hex, 16)) end function decodeURI(s) s = gsub(s, '%%(%x%x)', _) return s end end print(decodeURI('%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82'))