4

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?

3
  • When you say you want to "ignore" the accents, what do you mean? Do you mean that ole would match òle? Commented Jan 9, 2015 at 18:07
  • yes! that's what I want :) Commented Jan 9, 2015 at 18:16
  • Take a look at this link: stackoverflow.com/questions/5700636/… Commented Jan 9, 2015 at 18:30

2 Answers 2

11

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
Sign up to request clarification or add additional context in comments.

Comments

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 ));
});

3 Comments

Yes you shoud UpperCase all strings and then use this code
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?
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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.