为网址编码西里尔字母的最快方法 [英] Fastest way to encode cyrillic letters for url

查看:143
本文介绍了为网址编码西里尔字母的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您将以下链接复制到浏览器中

 http://be.wikipedia.org/wiki/Беларусь

它会显示Wiki文章。
但是,一旦您想从浏览器网址中将该链接(或包含西里尔文符号的任何其他链接)复制到记事本中,您将得到如下所示的内容:

  http://be.wikipedia.org/wiki/%D0%91%D0%B5%D0%BB%D0%B0%D1%80%D1%83%D1 %81%D1%8C 

您可以点击维基百科中包含西里尔字母的任何链接文本并尝试将其复制到记事本中。



所以,我的问题是:



什么是最正确的或将包含西里尔文字Беларусь的任何文本转换为%D0%91%D0%B5%D0%BB%D0%B0%D1 %80%D1%83%D1%81%D1%8C 或任何其他文本放入此类代码中,因此它是URL的有效部分?
是否有一个特殊的JavaScript函数用于此目的?

我已经检查过,它实际上是:西里尔文大写字母Б=(hex)D0 91代表UTF -8。这就是为什么它是%D0%91等等。

解决方案

您正在搜索的函数是 encodeURIComponent

  encodeURIComponent(Беларусь); 
//返回%D0%91%D0%B5%D0%BB%D0%B0%D1%80%D1%83%D1%81%D1%8C

它的对手是 decodeURIComponent ,它颠倒了这个过程。

  decodeURIComponent(%D0%91%D0%B5%D0%BB%D0%B0%D1%80%D1%83%D1%81%D1%图8C); 
//返回Беларусь


If you copy the link below into the browser

http://be.wikipedia.org/wiki/Беларусь 

it will show the Wiki article. But once you want to copy that link (or any other link that contains cyrillic symbols) from the browser url into the notepad, you'll get something like this:

http://be.wikipedia.org/wiki/%D0%91%D0%B5%D0%BB%D0%B0%D1%80%D1%83%D1%81%D1%8C

You can click on any link in the wikipedia that contains cyrillic letters in the text and try to copy it into the Notepad.

So, my question is:

What's the most correct or fastest way to convert any text that contains cyrillic word Беларусь into %D0%91%D0%B5%D0%BB%D0%B0%D1%80%D1%83%D1%81%D1%8C or any other text into such type of code so it is a valid part of the URL? Is there a special javascript function for that purpose?

I've checked, it is actually : cyrillic capital letter Б = (hex) D0 91 for UTF-8. That's why it is %D0%91 and so on.

解决方案

The function you're searching for is encodeURIComponent.

encodeURIComponent("Беларусь");
// returns "%D0%91%D0%B5%D0%BB%D0%B0%D1%80%D1%83%D1%81%D1%8C"

Its counterpart is decodeURIComponent which reverses this process.

decodeURIComponent("%D0%91%D0%B5%D0%BB%D0%B0%D1%80%D1%83%D1%81%D1%8C");
// returns "Беларусь"

这篇关于为网址编码西里尔字母的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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