JavaScript:与文化无关的不区分大小写的字符串比较 [英] JavaScript: Culture-independent case-insensitive string comparison

查看:84
本文介绍了JavaScript:与文化无关的不区分大小写的字符串比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果之前已经提出这个问题,请指出相关问题。

If this exact question has been asked before, please point me to the relevant question.

tl; dr:如何比较JavaScript中的两个字符串而忽略套管根据英语规则?

tl;dr: How does one compare two strings in JavaScript while ignoring casing according to English rules?

我的代码分析并比较来自两个不同来源的数据,每个来源对于关键字是高还是低都有不同的看法case,意味着需要不区分大小写的比较。但是,如果在其他文化中使用(例如土耳其及其字母I中臭名昭着的问题),我不希望系统中断。

My code analyzes and compares data from two different sources, each with different opinions about whether keywords should be upper or lower case, meaning that a case-insensitive comparison is required. However, I don't want the system to break if used in other cultures (such as Turkey and its notorious problems with the letter I).

JavaScript有什么办法吗?做一个独立于文化(读:英语)不区分大小写的字符串比较?

Does JavaScript have any way of doing a culture-independent (read: English) case-insensitive string comparison?

推荐答案


如何如果在其他文化中使用(例如土耳其及其字母I的臭名昭着的问题),那么在JavaScript中比较两个字符串而不会破坏吗? JavaScript是否有任何方法可以进行与文化无关的不区分大小写的字符串比较?

How does one compare two strings in JavaScript without breaking if used in other cultures (such as Turkey and its notorious problems with the letter I)? Does JavaScript have any way of doing a culture-independent case-insensitive string comparison?

是的,只需使用标准 .toLowerCase 通过区域设置不受影响的方法 a.toLowerCase()== b.toLowerCase()完全没问题。 规范甚至要求:

Yes, by simply using the standard .toLowerCase method which is not affected by locale settings. a.toLowerCase() == b.toLowerCase() is perfectly fine. The spec even mandates:


必须根据Unicode字符数据库中的大小写映射派生结果

无论设置如何,这绝对在所有系统中都是一致的。

This definitely is consistent across all systems regardless of their settings.

For尊重当前区域设置,您可以使用 .toLocaleLowerCase 。 MDN声明:

For respecting the current locale, you would use .toLocaleLowerCase explicitly. MDN states:


在大多数情况下,这将产生与toLowerCase()相同的结果,但对于某些语言环境,例如土耳其语,其大小写映射不遵循Unicode中的默认大小写映射,可能会有不同的结果。

In most cases, this will produce the same result as toLowerCase(), but for some locales, such as Turkish, whose case mappings do not follow the default case mappings in Unicode, there may be a different result.

但是,依赖于语言环境似乎效果不佳 ...

这篇关于JavaScript:与文化无关的不区分大小写的字符串比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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