问题描述
如果之前已经提出这个问题,请指出相关问题。
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?
推荐答案
是的,只需使用。 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:
无论设置如何,这绝对在所有系统中都是一致的。
This definitely is consistent across all systems regardless of their settings.
For尊重当前区域设置,您可以使用。 MDN声明:
For respecting the current locale, you would use .toLocaleLowerCase
explicitly. MDN states:
但是, ...
这篇关于JavaScript:与文化无关的不区分大小写的字符串比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!