简体   繁体   中英

Javascript RegEx UTF-8

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!"

 var myInput = document.getElementById("myInput"); function myFunction() { var text, answer = myInput.value.toLowerCase(); answer = answer.replace("my name is ", ""); switch (answer) { case "": text = "Please type something."; break; default: text = "Hello, " + CapitalizeName(answer) + "! Nice to meet you!"; } document.getElementById("reply").innerHTML = text; } function CapitalizeName(name) { let _array = name.split(" "); let n_array = []; _array.map(w => { w = w.charAt(0).toUpperCase() + w.slice(1); n_array.push(w); }); return n_array.join(" "); } 
 <p>What is your name?</p> <input id="myInput" type="text"> <button onclick="myFunction()">Go</button> <p id="reply"></p> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM