javascript转义非英语以外的特殊字符

发布于 2024-11-16 19:15:28 字数 242 浏览 0 评论 0原文

我在转义非英语字符时遇到问题。

例如,

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

output: ABC%20Farmac%E9utica%20Corporation 

但我只想转义除非英语字符(如 é)之外的特殊字符。 我们有什么逻辑可以忽略这些字符吗?

请帮忙,提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞清仓 2024-11-23 19:15:28

escape 函数返回以 Unicode 格式转义的文本。空格表示为 %20(十六进制)。如果我理解正确的话,你不想转义 é,在这种情况下,只想转义空格字符。我能看到这种情况的唯一方法是,您有一个包含那些您不想转义并引用的非英语字符的表。像这样的事情:

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;
}

是否有任何特殊原因为什么要选择性地转义字符?

The escape function returns the text escaped in the Unicode format. Space is represented as %20 (hexadecimal). 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文