2

I'm developing an app and I've been asked to compare strings, but text in string have special characters (spanish accents like "á", "é", "í", "ó" and "ú")

I already manage capitalization with toUpperCase(), but still, I want to be sure that I have no problem with accents.

What I have to do is to compare some words already saved in system and check if used typed any of them.

What I do is store the typed words in an array, and then proceed to analyze them in another function (yet to be implemented)

This is my function where I store the words the user types (it may change to make it more complete):

function clickNewWord(){
    var theWord = textField.value.toUpperCase();
    ArrayWrittenWords.push(theWord);
    textField.value = "";
}

PD: I'll take the opportunity to ask: What would be the correct coding to work with accents? UTF-8?

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
Sascuash
  • 3,661
  • 10
  • 46
  • 65

2 Answers2

10

Although its an old question however, for the sake of future googlers here is the best way to remove accent from a string:

var string = 'á é í ó ú';
string.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
>a e i o u
Ekeuwei
  • 273
  • 3
  • 7
3

You can convert them and then match them, let me if my example is clear :)

var stringInTheSystem = ['aaaa','bbbb'];// Array of string in your system;

var term = 'áaaa';// the word you want to compare it; 
term = term.replace(/á/g, "a"); 
term = term.replace(/é/g, "e"); 
term = term.replace(/í/g, "i"); 
term = term.replace(/ó/g, "o"); 
term = term.replace(/ú/g, "u"); 
var matcher = new RegExp( term, "i" );
$.grep( stringInTheSystem, function( value ) {
                  value = value.test || value.value || value;
                    console.log(matcher.test( value ));
});
Mouhamad Kawas
  • 370
  • 2
  • 12
  • Yes you shoud UpperCase all strings and then use this code – Mouhamad Kawas Jan 09 '15 at 18:23
  • Hey, while this code works fine when text with accents is in code, but when I have to read it from textarea with "textField.value.toUpperCase();" it doesn't work. Why is this happennig? – Sascuash Jan 09 '15 at 20:30
  • Please, as I told you in my last comment, I found other issue and I'd like to solve it without asking a new question (as you can see I only work in this project on fridays) – Sascuash Jan 16 '15 at 08:57