最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript - Ignore punctuation and spaces in a Switch Statement - Stack Overflow

programmeradmin2浏览0评论

I have a page with a question. The user will have to type the answer to that question in a textbox. I am using a switch statement to generate different feedback to different answers. I already managed to make it case insensitive.

Is there a way to also make it ignore punctuation and spaces?

This is the code I have:

function myFunction() {
    var text;
    var answers = document.getElementById("myInput").value.toLowerCase();
    switch (answers) {
        case "superman":
            text = "That is correct!";
            break;
        case "batman":
            text = "You must be kidding me...";
            break;
        default:
            text = "Wrong answer."
    }
    document.getElementById("ment").innerHTML = text;
}
<p>Who is Clark Kent?</p>
<input id="myInput" type="text">
<button onclick="myFunction()">Answer</button>
<p id="ment"></p>

I have a page with a question. The user will have to type the answer to that question in a textbox. I am using a switch statement to generate different feedback to different answers. I already managed to make it case insensitive.

Is there a way to also make it ignore punctuation and spaces?

This is the code I have:

function myFunction() {
    var text;
    var answers = document.getElementById("myInput").value.toLowerCase();
    switch (answers) {
        case "superman":
            text = "That is correct!";
            break;
        case "batman":
            text = "You must be kidding me...";
            break;
        default:
            text = "Wrong answer."
    }
    document.getElementById("ment").innerHTML = text;
}
<p>Who is Clark Kent?</p>
<input id="myInput" type="text">
<button onclick="myFunction()">Answer</button>
<p id="ment"></p>

I would like it to accept all the following answers as correct, without having to add extra cases:

"Superman", "superman", "Super Man", "Super man", "Super-Man!", "Super-man"...

Share Improve this question asked Sep 18, 2018 at 10:23 MikeMichaelsMikeMichaels 4942 gold badges6 silver badges27 bronze badges 1
  • possible duplicate of stackoverflow./questions/4374822/… – Aagam Jain Commented Sep 18, 2018 at 10:25
Add a ment  | 

3 Answers 3

Reset to default 4

You could use a regex to ignore everything else that is not an alphabet.

function myFunction() {
    var text;
    var answers = document.getElementById("myInput").value.toLowerCase();
    answers = answers.replace(/[^a-z]/g, "");
    switch (answers) {
        case "superman":
            text = "That is correct!";
            break;
        case "batman":
            text = "You must be kidding me...";
            break;
        default:
            text = "Wrong answer."
    }
    document.getElementById("ment").innerHTML = text;
}

You could match only letters and omit unwanted character. Then take convert to lower case.

function myFunction() {
    function getLetters(s) { return s.match(/[a-z]/gi).join('').toLowerCase(); }

    var text;
    var answers = document.getElementById("myInput").value.toLowerCase();
    switch (getLetters(answers)) {
        case "superman":
            text = "That is correct!";
            break;
        case "batman":
            text = "You must be kidding me...";
            break;
        default:
            text = "Wrong answer."
    }
    document.getElementById("ment").innerHTML = text;
}
<p>Who is Clark Kent?</p>
<input id="myInput" type="text">
<button onclick="myFunction()">Answer</button>
<p id="ment"></p>

use this:

var desired = stringToReplace.replace(/[^\w\s]/gi, '').toLowerCase();
发布评论

评论列表(0)

  1. 暂无评论