javascript - 如何将unicode字符串转换为ascii [英] javascript - how to convert unicode string to ascii
问题描述
如何将unicode字符串转换为ascii,为友好的url创建一个很好的字符串?
How to convert unicode string to ascii to make a nice string for a friendly url?
推荐答案
只有一个< a href =http://labs.apache.org/webarch/uri/rfc/rfc3986.html#percent-encoding\"rel =noreferrer>简短的字符列表,可以安全地在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 / "-" / "." / "_" / "~"
所有其他字符都必须删除(如果你'创造一个slu)或逃脱。
All the other characters will have to be either removed (if you're creating a "slug") or escaped.
可以使用正则表达式 / [^ a-zA-Z0-9 -._〜] /
。
Removal can be done with the regex /[^a-zA-Z0-9-._~]/
.
可以使用 encodeURIComponent()
完成转义。
如果您希望获得相当于ICONV的音译(即将é
转换为 e
和€
进入 EUR
),你必须自己动手,尽管你可以利用现有解决方案并可能转换音译表为JS格式。
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.
这篇关于javascript - 如何将unicode字符串转换为ascii的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!