I know JavaScript’s RegEx only supports the \b word boundary class on ASCII strings. But is there anything I can do to make it work with UTF-8 characters?
I have read several posts about it here on stackoverflow, and tried a few methods like the one described here.
But I still cannot make it work.
I have a page where the user is asked his name.
After typing it in a textbox, a reply will show up, using part of the value from the textbox to greet the user, and ignoring some other parts of it.
If the user types "My name is John", the reply will be "Hello, john! Nice to meet you!".
This works fine in English, but the page will be on several different languages that use characters like é á ó ã ñ... How can I make it ignore these characters when they are not part of the name?
This is what I am working with:
function myFunction() {
var text;
var answer = document.getElementById("myInput").value.toLowerCase();
answer = answer.replace(/[^a-z0-9çéã\s]/g, "");
answer = answer.replace(/\b(my|name|is|)\b/g, '').trim();
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>
I tried .replace(/á|é|ó|\b(my|name|is)\b/g, '')
But this removes those characters when they are part of words/names and not when they are used as single words (which is what I want).
Following the example on that link, I also tried:
answer = answer.replace(/(^|[ \n\r\t.,'"+!?-]+)(é|á|ó|ñ|õ|hello|my|name|is)([ \n\r\t.,'"\+!?-]+|$)/g, '$1$3').trim();
But it still does not work as intended...
How can I fix this?
Again: I don't know if this is the answer you are looking for. This will also recapitalize the first letter of the name. So if I'm writing "My name is Salvador Dalí" the answer is: "Hello, Salvador Dalí! Nice to meet you!"