开发者

javascript - how to convert unicode string to ascii [duplicate]

This question already has answers here: Efficiently replace all accented characters in a string? (22 answers) 开发者_Python百科 Closed 6 years ago.

How to convert unicode string to ascii to make a nice string for a friendly url?


There is only a short list of characters that can be safely carried through in a path component of a URL.

unreserved  = ALPHA / DIGIT / "-" / "." / "_" / "~"

All the other characters will have to be either removed (if you're creating a "slug") or escaped.

Removal can be done with the regex /[^a-zA-Z0-9-._~]/.

Escaping can be done with encodeURIComponent().

If you wish to achieve an equivalent of ICONV transliteration (that is, turning é into e and into EUR), you'll have to do your own, although you can leverage existing solutions and perhaps transform a transliteration table to JS format.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜