简体   繁体   English

javascript转义特殊字符,非英语除外

[英]javascript escape special characters except non english

I have a problem with unescaping non-english characters. 我遇到了无法转义非英语字符的问题。

For Example, 例如,

var text1 = "ABC Farmacéutica Corporation".
text1 = escape(text1);

output: ABC%20Farmac%E9utica%20Corporation 

But I would like to escape only special characters other than non-english characters like é. 但是我只想转义非é等非英语字符以外的特殊字符。 Do we have any logic for these characters to be ignored? 我们有什么逻辑可以忽略这些字符吗?

Please help, Thanks in advance. 请帮助,在此先感谢。

The escape function returns the text escaped in the Unicode format. 转义函数返回以Unicode格式转义的文本。 Space is represented as %20 (hexadecimal). 空间表示为%20(十六进制)。 If I understand you correctly you don't want to escape é, in this case, only the space character. 如果我正确地理解了您的意思,那么您就不想逃避é了,在这种情况下,只希望转义空格字符。 The only way I can see this is possible is that you have a table containing those non-english characters that you don't want to be escaped and refer to it. 我看到这种情况的唯一方法是,您有一个表,其中包含您不想转义的那些非英语字符并对其进行引用。 Something like this: 像这样:

var dontEscape = "éöå....and_so_on";
var text = "ABC Farmacéutica Corporation";
var escaped = "";
for (var i = 0; i < text.length; i++) {
  var test = text.substring(i, i+1); // charAt is unsafe with unicode chars
  escaped = test.indexOf(test.toLowerCase()) == -1 ? escape(test) : test;
}

Is there any special reason for why you want to escape characters selectively? 为什么要有选择地转义字符有任何特殊原因吗?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM