栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python的urllib.quote()和urllib.unquote()的等效Javascript函数

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python的urllib.quote()和urllib.unquote()的等效Javascript函数

好的,我想我将使用一组混合的自定义函数:

编码:使用enpreURIComponent(),然后放回斜杠。
解码:对找到的所有%hex值进行解码。

这是我最终使用的内容的更完整变体(它也可以正确处理Unipre):

function quoteUrl(url, safe) {    if (typeof(safe) !== 'string') {        safe = '/';    // Don't escape slashes by default    }    url = enpreURIComponent(url);    // Unescape characters that were in the safe list    toUnenpre = [  ];    for (var i = safe.length - 1; i >= 0; --i) {        var enpred = enpreURIComponent(safe[i]);        if (enpred !== safe.charAt(i)) {    // Ignore safe char if it wasn't escaped toUnenpre.push(enpred);        }    }    url = url.replace(new RegExp(toUnenpre.join('|'), 'ig'), depreURIComponent);    return url;}var unquoteUrl = depreURIComponent;    // Make alias to have symmetric function names

请注意,如果在编码时(你并不需要“安全”的字符

'/'
默认的Python),那么你可以使用内置的
enpreURIComponent()
depreURIComponent()
直接的功能。

另外,如果字符串中包含Unipre字符(即,代码点> =
128的字符),则为了保持与Javascript的兼容性

enpreURIComponent()
,Python
quote_url()
必须为:

def quote_url(url, safe):    """URL-enpres a string (either str (i.e. ASCII) or unipre);    uses de-facto UTF-8 encoding to handle Unipre prepoints in given string.    """    return urllib.quote(unipre(url).enpre('utf-8'), safe)

并且

unquote_url()
将是:

def unquote_url(url):    """Depres a URL that was enpred using quote_url.    Returns a unipre instance.    """    return urllib.unquote(url).depre('utf-8')


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/626465.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号