如何在 JavaScript 中对字符串进行不区分大小写的字符串比较
在本教程中,我们将学习如何在 JavaScript 中对字符串进行不区分大小写的字符串比较。不区分大小写的简单含义是,即使单词或字符串以小写或大写形式书写,其含义也应相同。在 Google 搜索栏中可以观察到这种不区分大小写的比较,其中,如果用户输入"tUtoriAlsPOint"或"Tutorialspoint",结果是相同的,同样,它也用于搜索栏中。有 4 种方法可以实现此目的,
toUpperCase()
toLowerCase()
localeCompare()
RegExp()
使用 toUpperCase() 方法
当我们对字符串使用 toUpperCase() 方法时,字符串将转换为全大写字符串。不会对数字、特殊字符({、}、>、$ 等)和已经大写的字母进行任何更改。
语法
str.toUpperCase();
参数
str − 需要转换为大写的文本
示例
在下面的示例中,我们选择了两个相同但大小写不同的字符串(小写和大写)。我们使用 toUpperCase() 方法将两个字符串转换为大写,然后再进行比较。
<html> <body> <h2>Case insensitive comparison using <i>toUpperCase()</i> method</h2> <div id="str1"></div> <script> var str1 = "TexT with uPPer CAsE aND loWeR cASe"; var str2 = "TexT with Upper Case aND LoWER CAse"; var output = document.getElementById("str1"); output.innerHTML += "str1 = " + str1+ "<br/>"; output.innerHTML += "str2 = " + str2 + "<br/>"; if (str1.toUpperCase() === str2.toUpperCase()) { output.innerHTML += "The strings are converted to Uppercase and they are equal" + "<br/>"; } else { output.innerHTML += "The strings are converted to Uppercase and they are not equal"; } </script> </body> </html>
使用 ToLowerCase() 方法
与 toUpperCase() 类似,此方法中字符串将转换为全小写字符串。不会对数字、特殊字符({、}、>、$ 等)或已经为小写的字母进行任何更改
语法
str.toLowerCase();
参数
str − 转换为小写的文本
示例
在下面的示例中,我们使用了两个相同但大小写不同的字符串(小写和大写)。在相互比较之前,两个字符串都使用 toLowerCase() 方法转换为小写。
<html> <body> <h2>Case insensitive comparison using <i>toLowerCase()</i> method</h2> <div id="str1"></div> <script> var str1 = "TutorialsPoint"; var str2 = "tuTorialspoInt"; var output = document.getElementById("str1"); output.innerHTML += "str1 = " + str1 + "<br/>"; output.innerHTML += "str2 = " + str2 + "<br/>"; if (str1.toLowerCase() === str2.toLowerCase()) { output.innerHTML += "The strings are converted to Lowercase and they are equal"; } else { output.innerHTML += "The strings are converted to Uppercase and they are not equal"; } </script> </body> </html>
使用 localeCompare() 方法
localeCompare() 方法返回一个数字(可以是 0、-ve 数字、+ve 数字),该数字表示引用字符串在排序顺序中位于给定字符串之前、之后还是相同。当有可能比较两种不同的语言时,这是一个更好的选择,我们可以根据需要自定义比较。
语法
localeCompare(compareString, locales, options)
参数
compareString − 将参考字符串与此字符串进行比较
locales − 语言标记。
options − 您应该输入您正在使用的特定语言环境。
示例
在下面的例子中,我们选择了两个相同的字符串,但大小写和语言不同。 localeCompare() 在比较两个字符串后,如果两个字符串相等,则返回 0。
<html> <body> <h2>Case insensitive comparison using <i>localeCompare()</i> method</h2> <div id="str1"></div> <script> var str1 = "apPlE"; var str2 = "áppLE"; var output = document.getElementById("str1"); function caseInsensitive(a, b) { return a.localeCompare(b, 'en', { sensitivity: 'base' }) === 0; } output.innerHTML += "str1 = " + str1 + "<br/>"; output.innerHTML += "str2 = " + str2 + "<br/>"; output.innerHTML += "str1 === str2 is " + caseInsensitive(str1, str2); </script> </body> </html>
使用 RegExp() 对象
RegExp() 代表正则表达式,它是用于模式匹配的字符模式。在本例中,我们使用 test() 函数进行搜索,查看模式是否与提供的文本匹配。如果文本匹配,则函数将返回 true。
语法
RegExp('^' + str + '$', 'i')
参数详细信息
^ − 字符串的开头。
$ − 字符串的结尾。
str − 需要遵循的模式。
i − 标志变量,表示是否应忽略某个案例。同时将模式与字符串进行匹配。
示例
在此示例中,我们使用 RegExp() 对象创建模式,并使用 test() 函数查看模式是否与字符串匹配。test() 函数根据比较结果和字符串返回布尔值
<html> <body> <h2>Case insensitive comparison using <i>RegEx()</i> object</h2> <div id="str1"></div> <script> var str1 = "tutorialspoint"; var str2 = "TUTORIALSPOINT"; var output = document.getElementById("str1"); output.innerHTML += "str1 = " + str1 + "<br/>"; output.innerHTML += "str2 = " + str2 + "<br/>"; let pattern = new RegExp('^' + str1 + '$', 'i'); if (pattern.test(str2)) { output.innerHTML += "str1 and str2 are same"; } else { output.innerHTML += "str1 and str2 are not same"; } </script> </body> </html>