Python的urllib.quote()和urllib.unquote()的等效Javascript函数 [英] Equivalent Javascript Functions for Python's urllib.quote() and urllib.unquote()

查看:578
本文介绍了Python的urllib.quote()和urllib.unquote()的等效Javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Python的 urllib,是否有任何等效的Javascript函数。 quote() urllib.unquote()



我遇到的最接近的是 escape() encodeURI() encodeURIComponent() (及其相应的非编码功能),但他们不t / b>

谢谢,

Cameron


好吧,我想我会去做一个混合的自定义函数集:



编码:使用encodeURIComponent(),然后将斜杠重新插入。

解码:解码找到的任何%十六进制值。



这是一个更完整的变体,我最终使用(它也正确处理Unicode):

 函数quoteUrl(url,safe){
if(typeof(safe)!=='string'){
safe ='/'; //默认情况下不要退出斜杠
}

url = encodeURIComponent(url);

//取消对安全列表中的字符
toUnencode = [];
for(var i = safe.length - 1; i> = 0; --i){
var encoded = encodeURIComponent(safe [i]);
if(encoded!== safe.charAt(i)){//如果没有转义,则忽略安全字符
toUnencode.push(encoded);
}
}

url = url.replace(new RegExp(toUnencode.join('|'),'ig'),decodeURIComponent);

return url;
}


var unquoteUrl = decodeURIComponent; //使别名具有对称函数名称

请注意,如果不需要安全字符在Python中默认编码('/')时,您只需使用内置的 encodeURIComponent() decodeURIComponent()函数直接。



另外,如果有Unicode字符(即具有codepoint> = 128的字符),然后为了保持与JavaScript的 encodeURIComponent()的兼容性,Python quote_url()将必须:

$ b

  def quote_url(url,safe):
URL编码一个字符串ASCII)或unicode);
使用事实上的UTF-8编码来处理给定字符串中的Unicode代码点

return urllib.quote(unicode(url)).encode(' utf-8'),安全)

unquote_url()将是:

  def unquote_url(url):
解码使用quote_url编码的网址。
返回一个unicode实例。

return urllib.unquote(url).decode('utf-8')


Are there any equivalent Javascript functions for Python's urllib.quote() and urllib.unquote()?

The closest I've come across are escape(), encodeURI(), and encodeURIComponent() (and their corresponding un-encoding functions), but they don't encode/decode the same set of special characters as far as I can tell.

Thanks,
Cameron

解决方案

OK, I think I'm going to go with a hybrid custom set of functions:

Encode: Use encodeURIComponent(), then put slashes back in.
Decode: Decode any %hex values found.

Here's a more complete variant of what I ended up using (it handles Unicode properly, too):

function quoteUrl(url, safe) {
    if (typeof(safe) !== 'string') {
        safe = '/';    // Don't escape slashes by default
    }

    url = encodeURIComponent(url);

    // Unescape characters that were in the safe list
    toUnencode = [  ];
    for (var i = safe.length - 1; i >= 0; --i) {
        var encoded = encodeURIComponent(safe[i]);
        if (encoded !== safe.charAt(i)) {    // Ignore safe char if it wasn't escaped
            toUnencode.push(encoded);
        }
    }

    url = url.replace(new RegExp(toUnencode.join('|'), 'ig'), decodeURIComponent);

    return url;
}


var unquoteUrl = decodeURIComponent;    // Make alias to have symmetric function names

Note that if you don't need "safe" characters when encoding ('/' by default in Python), then you can just use the built-in encodeURIComponent() and decodeURIComponent() functions directly.

Also, if there are Unicode characters (i.e. characters with codepoint >= 128) in the string, then to maintain compatibility with JavaScript's encodeURIComponent(), the Python quote_url() would have to be:

def quote_url(url, safe):
    """URL-encodes a string (either str (i.e. ASCII) or unicode);
    uses de-facto UTF-8 encoding to handle Unicode codepoints in given string.
    """
    return urllib.quote(unicode(url).encode('utf-8'), safe)

And unquote_url() would be:

def unquote_url(url):
    """Decodes a URL that was encoded using quote_url.
    Returns a unicode instance.
    """
    return urllib.unquote(url).decode('utf-8')

这篇关于Python的urllib.quote()和urllib.unquote()的等效Javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆