简体   繁体   English

转义字符串中的字符免除

[英]Exempting characters in an escaped string

I have a little function that makes URL arguments out of an object: 我有一个小功能,可以使对象产生URL参数:

function MkArgs(o) {
    var ret = '?';
    for (var i in o) {
        ret += i + '=' + escape(o[i]) + '&';
    }
    return ret.substr(0, ret.length - 1);
}

which I then can call like this: 然后我可以这样称呼它:

MkArgs({
    protocol: 'wsfederation',
    realm: 'https://www.x.com/',
    fedRes: 'Home/FederationResult',
    context: '~/Home/FAQ',
    version: '1.0',
    callback: '?'
});

to produce the following: 产生以下内容:

?protocol=wsfederation&realm=https%3A//www.x.com/&fedRes=Home/FederationResult&context=%7E/Home/FAQ&version=1.0&callback=%3F

everything is fine except that I don't want the last argument escaped ie I want: 一切都很好,除了我不想逃避最后一个参数,即我想要:

callback=?

instead of 代替

callback=%3F

is there any way I can indicate that within the string? 有什么办法可以指示字符串中的内容吗? I tried '\\?' 我试过'\\?' but that doesn't do it and haven't found any references as to how to protect a piece of string from escaping... 但这并没有做到,也没有找到任何有关如何保护一段字符串不逃逸的参考...

  • e Ë

The escape or encodeURIComponent functions don't have any way of "skipping" certain characters. escape或encodeURIComponent函数没有“跳过”某些字符的任何方式。 So, all you can do is to either avoid calling the encode function when you don't want to or replace the chars you don't want encoded, call encode and then put the original chars back again. 因此,您所能做的就是避免在您不想使用时调用encode函数,或者替换您不想编码的字符,调用encode然后将原始字符再次放回原处。

If you want to skip escaping the whole value for a particular key, you can just check for the particular keys that you don't want to escape and handle appropriately like this: 如果要跳过转义特定键的整个值,则只需检查不想转义的特定键,并按以下方式进行适当处理:

function MkArgs(o) {
    var ret = '?';
    for (var i in o) {
        var val = o[i];
        if (i != "callback") {
            val = encodeURIComponent(val);
        }
        ret += i + '=' + val + '&';
    }
    return ret.substr(0, ret.length - 1);
}

If you want to skip just certain characters, then you can replace them with some unique sequence, escape and then put them back: 如果只想跳过某些字符,则可以用一些唯一的序列替换它们,先转义然后放回它们:

function MkArgs(o) {
    var ret = '?';
    for (var i in o) {
        var val = o[i];
        if (i == "callback") {
            val = val.replace(/\?/, "--xx--");  // replace with unique sequence
            val = encodeURIComponent(val);
            val = val.replace(/--xx--/, "?");   // put orig characters back
        } else {
            val = encodeURIComponent(val);
        }
        ret += i + '=' + val + '&';
    }
    return ret.substr(0, ret.length - 1);
}

FYI, note I've switched to using encodeURIComponent() which is recommended over the deprecated escape() because escape() doesn't work for non-ascii characters. 仅供参考,请注意,我已改用已弃用的escape()推荐使用的encodeURIComponent() ,因为escape()不适用于非ascii字符。

The MkArgs function is your own; MkArgs函数是您自己的。 change it to include an escape mechanism. 对其进行更改以包括转义机制。 I would advise against using backslash, though. 不过,我建议不要使用反斜杠。 If this is just your own code, perhaps it would be enough to put in a hackish special case. 如果这只是您自己的代码,那么放置一个骇人的特殊情况就足够了。

That's a pretty special case. 那是一个非常特殊的情况。 Maybe you should change your function: 也许您应该更改功能:

function MkArgs(o, isJSONP) {
    var ret = '?';
    for (var i in o) {
        var val = o[i];
        val = escape(val);
        ret += i + '=' + val + '&';
    }
    return ret.substr(0, ret.length - 1) + isJSONP ? '&callback=?':'';
}

and call it: 并称之为:

MkArgs({
  protocol: 'wsfederation',
  realm: 'https://www.x.com/',
  fedRes: 'Home/FederationResult',
  context: '~/Home/FAQ',
  version: '1.0'
}, true);
function MkArgs(o) {
    var ret = '?';
    var lastEl = '';
    for (var i in o) {
        ret += i + '=' + escape(o[i]) + '&';
        lastEl = o[i];
    }
    return ret.substr(0, ret.length - 1 - lastEl.length) + lastEl;
}

this works for the last element in the object. 这适用于对象中的最后一个元素。

EDIT : It seems that in a classic for in loop, javascript does not have a precise order in which it loops over the object props, so the above solution is not guaranteed to work. 编辑 :似乎在经典的for in循环中,javascript没有精确的顺序来循环对象props,因此不能保证上述解决方案能够正常工作。
In this case you have 2 solutions : 在这种情况下,您有2个解决方案:

  • If you know which property you want to "protect" from escaping, you should check for that prop in the loop and specifically not escape it : 如果知道要“保护”转义的属性,则应在循环中检查该道具,尤其不要对其进行转义:

     for (var i in o) { if(i=="myProp") // unescape else // escape } 
  • If you do not now the property, but you want only the last one added into the query, you can do something like this (after building the query) : 如果您现在不使用该属性,但是只希望将最后一个属性添加到查询中,则可以执行以下操作(在构建查询之后):

     var q = MkArgs(o); var parts = q.split('='); var toUnescape = parts[parts.length-1]; q = q.substring(0,q.length - toUnescape.length) + unescape(toUnescape); 

thanks everyone for the replies. 感谢所有人的回复。 what I ended up doing was: 我最终要做的是:

function MkArgs(o) {
    var ret = '?';
    for (var i in o) {
        ret += i;
        if (o[i]) ret += '=' + escape(o[i]);
        ret += '&';
    }
    return ret.substr(0, ret.length - 1);
}

then calling it like: 然后像这样调用它:

MkArgs({
    protocol: 'wsfederation',
    realm: 'https://www.x.com/',
    fedRes: 'Home/FederationResult',
    context: '~/Home/FAQ',
    version: '1.0',
    'callback=?': null
});

that way I don't rely on the values but the keys to make the distinction. 这样,我就不依赖值而是依赖键来区分。 not really pretty but it's the best I could think of 不是很漂亮,但这是我能想到的最好的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 字符串中的所有字符均被转义 - All characters in string are escaped 正则表达式解析带有转义字符的字符串 - regex to parse string with escaped characters Javascript十六进制转义字符串返回到Cocoa App,但没有转义字符 - Javascript Hex Escaped String returned to Cocoa App without Escaped Characters 用空格分割字符串,保留转义字符 - Split string by whitespace, keeping escaped characters 将带有转义字符和ASCII值的字符串转换为十六进制 - Convert string with escaped characters and ASCII values into HEX 将转义字符视为单个字符,查找字符串中的字符数 - Find the count of the characters in a string considering escaped characters as single character javascript字符串中带有转义unicode字符的字符串和未转义的unicode字符有什么区别? - What is the difference between a string with escaped unicode characters and non-escaped unicode characters in javascript strings? 如何编写一个没有字符转义的长字符串文字? - How to write a long string literal in which no characters are escaped? 将字符串解析为一系列转义的十六进制字符的方法是什么? - What would be a way to parse a string into series of escaped hex characters? 北欧角色在JavaScript中逃脱 - Nordic characters escaped in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM