Lua decodeURI(luvit)如何实现URL解码?
- 内容介绍
- 相关推荐
本文共计286个文字,预计阅读时间需要2分钟。
在Lua(Luvit)项目中,您可以使用`lunr`库来模拟JavaScript中的`decodeURI`或`decodeURIComponent`功能。以下是一个简单的示例:
lualocal lunr=require(lunr)local decode=lunr.util.decode_uri
-- 使用decodeURIlocal result1=decode('привет') -- 输出: привет
-- 使用decodeURIComponentlocal result2=decode('привет') -- 输出: привет
请注意,这里的`lunr`库并不是Lua标准库的一部分,您可能需要先安装它。但根据您的要求,这里只提供了代码示例。
我想在我的Lua(Luvit)项目中使用decodeURI或decodeURIComponent,就像在 JavaScript中一样.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'))
本文共计286个文字,预计阅读时间需要2分钟。
在Lua(Luvit)项目中,您可以使用`lunr`库来模拟JavaScript中的`decodeURI`或`decodeURIComponent`功能。以下是一个简单的示例:
lualocal lunr=require(lunr)local decode=lunr.util.decode_uri
-- 使用decodeURIlocal result1=decode('привет') -- 输出: привет
-- 使用decodeURIComponentlocal result2=decode('привет') -- 输出: привет
请注意,这里的`lunr`库并不是Lua标准库的一部分,您可能需要先安装它。但根据您的要求,这里只提供了代码示例。
我想在我的Lua(Luvit)项目中使用decodeURI或decodeURIComponent,就像在 JavaScript中一样.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'))

