如何进行不区分大小写的字符串比较?

5
我是一个有用的助手,可以翻译文本。
在我的网站上有一个文本框,用户可以输入命令。但问题是这些命令区分大小写。如何使命令不区分大小写?这是我的代码:
JavaScript:
function showAlert() {

    var txtCtrl = document.getElementById("textbox1");

    var txtVal = txtCtrl.value;

    if (txtVal == '') {
        alert('Please fill in the text box. For a list of commands type "Help" into the text box.');
    }else if (txtVal == 'Start' || txtVal == 'start') {
        alert('Hello. What would you like me to do?');
    }else if (txtVal === 'Weather' || txtVal === 'weather') {
        window.location = "https://www.google.com/#q=weather";
    }else if (txtVal === 'Time' || txtVal === 'time') {
        alert('The current time according to your computer is' + formatTime(new Date()));
    }else if (txtVal === 'Help' || txtVal === 'help') {
        window.location = "help/index.html";
    }else if (txtVal === 'Donate' || txtVal === 'donate') {
        window.location = "donate/index.html";
    }else if (txtVal === 'www.' || txtVal === 'WWW.') {

    }else{
        alert('Sorry, I do not reconise that command. For a list of commands, type "Help" into the text box.');
    }
}
//Show time in 24hour format
function showTime(){
  var today = new Date();
  var h = today.getHours();
  var m = today.getMinutes(); 
  return [ h, m ].join(':')
}
//Show time in 12hour format
var formatTime = (function () {
    function addZero(num) {
        return (num >= 0 && num < 10) ? "0" + num : num + "";
    }

    return function (dt) {
        var formatted = '';

        if (dt) {
            var hours24 = dt.getHours();
            var hours = ((hours24 + 11) % 12) + 1;
            formatted = [formatted, [addZero(hours), addZero(dt.getMinutes())].join(":"), hours24 > 11 ? "PM" : "AM"].join(" ");            
        }
        return formatted;
    }
})();

还有 HTML:

<!doctype html>
<html>
<head>
<title>Random Project</title>
</head>
<style type="text/css">
body {
    margin:0;
    font-family: arial, sans-serif;
    padding-top: 80px;
}
.container {
    margin:auto;
    text-align:center;
}
.logo {
    margin:auto;
    width:512px;
    text-align:center;
    padding-bottom:0px;
}
#textbox1 {
    border: 1px solid #ddd;
    border-radius: 0px 0px 0px 0px;
    width: 512px;
    padding-top: 5px;
    padding-bottom: 5px;
    padding-left: 7px;
    padding-right: 7px;
    height: 20px;
    font-size: 16px;
    outline: none;
    font-family: arial, sans-serif;
}
#textbox1:focus {
    border: 1px solid #0266C8;
}
#button1 {
    margin-top: 22px;
    padding: 2px 10px 2px 10px;
    outline:none;
    background-color:#eee;
    border:1px solid #ddd;
    border-radius:2px;
}
#button1:hover {
    background-color: #f5f5f5;
}
#button1:focus {
    border: 1px solid #0266C8;
}
#button1_text {
    font:bold 11px/27px Arial,sans-serif!important;
    color:#333;
}
.information {
    font-size:12px;
    color:#555;
}
.separator {
    height:100px;
}
.tip {
    color:green;
    font-weight:bold;
    font-size:12px;
    margin-top:10px;
}
.tip_text {
    color:#111;
    font-weight:normal;
}
</style>
<body>
<div class="container">
    <img class="logo" src="logo.png" width="450" height="110" alt="Random Project">
    <input type="text" name="textbox1" value="" spellcheck="false" dir="ltr" placeholder="Type here" id="textbox1"><br>
    <button id="button1" name="button1" aria-label="Help me" onClick="showAlert();">
        <span id="button1_text">Help me</span>
    </button>
    <div class="separator"></div>
    <span class="information">&copy; Copyright DemDevs 2013. All rights reserved. Made by Omar Latreche<br><a href="donate/index.html">Donate now</a></span>
    <div class="tip">
        <span class="tip">Tip: </span><span class="tip_text">The commands are NOT case sensitive</span>
    </div>
</div>
<div class=""></div>
</body>
</html>

任何帮助都将不胜感激。
谢谢,Omar!
编辑:
我弄清楚了,我创建了两个新变量:
var txtValUpper = txtVal.toUpperCase();

var txtValLower = txtVal.toLowerCase();

然后将它们放入代码中,例如:
if (txtValUpper == 'start' || txtValLower == 'start') 
{
    alert('Hello. What would you like me to do?');
}

将命令值和要比较的值转换为大写或小写。 - Jite
@Juhana:当有重复发布时,您真的很擅长找到原始内容。我相信您没有得到足够的感谢,所以:谢谢您。 - T.J. Crowder
@T.J.Crowder 谢谢,有时候你就知道这个问题之前肯定被问过无数次 :) - JJJ
2
欢迎来到Stack Overflow。很高兴看到一个新手提供了与他们问题相关的代码,但是哇,我们真的需要看到所有的CSS吗? - nnnnnn
3个回答

9

要进行大小写不敏感的字符串比较,首先将它们转换为一致的大小写。

if ("Foo".toLowerCase() === "fOo".toLowerCase()) 

3
请注意,这种方法并不适用于所有语言,比如德语。 - user1248084

4

您可以将字符串转换为大写并进行比较

if(txtVal.toUpperCase() === "Donate".toUpperCase())

2

在这一行之后

var txtVal = txtCtrl.value;
txtVal = txtVal.toLowerCase();

5
然后,提问者应该将他/她所比较的内容全部改为小写字母。现在的回答实际上会导致错误。 - T.J. Crowder

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接