1

I have a page where the user is asked his name.

After typing it in a textbox, a reply will show up, using the value from the textbox to greet the user.

If the user just types his name, everything is fine, but if he types something like "My name is John", the reply will be "Hello, my name is john! Nice to meet you!", which I don't want to happen.

Is there a way to get the user input, but to exclude some words, like "my", "name", "is"...

This is what I have:

function myFunction() {
    var text;
    var answer = document.getElementById("myInput").value.toLowerCase();
    answer = answer.replace(/[^a-z0-9\s]/g, "");

    switch(answer) {
        case "":
            text = "Please type something.";
        break;
        default:
        text = "Hello, " + answer + "! Nice to meet you!";
    }
    document.getElementById("reply").innerHTML = text;
}
<p>What is your name?</p>

<input id="myInput" type="text">

<button onclick="myFunction()">Go</button>

<p id="reply"></p>
MikeMichaels
  • 454
  • 1
  • 6
  • 25

3 Answers3

4

Use a regular expression replacement to remove those words.

answer = answer.replace(/\b(my|name|is)\b/g, '').trim();
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • That works. Thank you very much! By the way, since I have to set the value to lowercase to avoid having something like "replace(/\b(my|name|is|My|Name|Is|MY|NAME|IS)\b/g, '')", is there a way to still capitalize the first letters of the words that were not excluded (the name)? – MikeMichaels Sep 22 '18 at 09:32
  • See https://stackoverflow.com/questions/196972/convert-string-to-title-case-with-javascript – Barmar Sep 22 '18 at 09:33
  • Or you could leave the input in mixed case, and use the `i` flag in the regexp to do case-insensitive matching. – Barmar Sep 22 '18 at 09:34
  • 2
    `replace(/\b(my|name|is)\b/gi, '')` – Barmar Sep 22 '18 at 09:34
  • Thank you very much, again! One final question, if it's not asking too much... Is there a way to also exclude single special characters (like ç ñ é...) when they are used alone, but not when they are part of a word? .replace(/\b(my|name|is|ç|é|ñ|)\b/g, '') removes them from words, but not when they are alone... – MikeMichaels Sep 22 '18 at 10:51
  • Don't put them between `\b`. `.replace(/ç|é|ñ|\b(my|name|is)\b/g, '')` – Barmar Sep 22 '18 at 10:53
  • I just tried that, but it still removes those characters when they are used as part of words, like Chloé, or Béringer (french names). – MikeMichaels Sep 22 '18 at 11:07
  • The problem is that JavaScript doesn't consider those characters to be word characters. So when they're next to word characters, that's treated as a word boundary, and `\b` matches there. I'm not sure how to fix that. – Barmar Sep 22 '18 at 11:12
  • See http://breakthebit.org/post/3446894238/word-boundaries-in-javascripts-regular for a workaround. – Barmar Sep 22 '18 at 11:13
  • Thank you for your continued help. I am a beginner and don't know much about regex. I don't know how to work around the problem, following the article you linked to. How could I apply it to my code? – MikeMichaels Sep 22 '18 at 20:16
  • Replace the first `\b` with `(^|[ \n\r\t.,'"+!?-]+)` and the second `\b` with `([ \n\r\t.,'"\+!?-]+|$)` – Barmar Sep 22 '18 at 20:19
  • Like this? answer = answer.replace(/(^|[ \n\r\t.,'"+!?-]+)(hello|my|name|is)([ \n\r\t.,'"\+!?-]+|$)/g, '').trim(); Because I tried it, and still doesn't work properly. "Hello, my name is Chloé" returns "Hello, myis chloé.! Nice to meet you!" – MikeMichaels Sep 22 '18 at 20:26
  • You need to copy the capture groups with the delimiters to the replacement. `answer = answer.replace(/(^|[ \n\r\t.,'"+!?-]+)(hello|my|name|is)([ \n\r\t.,'"\+!?-]+|$)/g, '$1$3').trim();` – Barmar Sep 22 '18 at 20:32
  • BTW, when you put code in comments, wrap it in backticks to make it more readable. – Barmar Sep 22 '18 at 20:32
1

@Barmar's answer works if you're just trying to remove a few words and you don't mind hard-coding those into your code.

If your list grows or if it comes from a database, you might find this approach works better.

const banned_words = ["my", "name", "is"];
const str = "MY name is Bonzo";

const newStr = banned_words.reduce(function(a, v) {
    // Make it case-insensitive
    var regex = new RegExp(v, "i");

    // Replace each banned word with the empty string
    return a.replace(regex, "")
  },
  str // Start with the original string

).trim(); // Then get rid of unwanted spaces.

console.log(newStr);
Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74
0

You can do like this:

var string = "my name is Jonh";
var words = ['my', 'name', 'is'];

words.forEach(function(word) {
    string = string.replace(word,'');

});

console.log(string);
NicolaPez
  • 567
  • 1
  • 4
  • 27