Remove accents (javascript)

Asked

Viewed 30,259 times

13

I have this script below with the function toLowerCase();

I would like to add another function to the function toLowerCase();

How would the line look with the two functions together?

<script language="">

function kw_list ()

{

this.keywords = new Array ();

this.num_words = 0;

this.query = "";

this.original_query = "";

this.query_length = 0;

this.possible_points = 0;

this.multiple = points_title + points_keywords + points_description;

this.get_words = get_query;

this.no_query = no_query_found;

}


function get_query ()

{

this.query = top.location.search.substring (top.location.search.indexOf ('=') + 1);

while ((the_plus = (this.query.indexOf ("+", 0))) != -1)

{

this.query_length = this.query.length;

this.query = this.query.substring (0, the_plus) + " " + this.query.substring (the_plus + 1);

}

this.original_query = unescape (this.query);

this.query = this.original_query.toLowerCase ();

this.query_length = this.query.length;  

if (this.query != "")

{

var query_pointer = 0;

var end_word = 0;

var at_end = 0;

while ((this.num_words <= (max_keywords - 1)) && (! at_end))

{

end_word = this.query.indexOf (" ", query_pointer);

if (end_word == query_pointer)

query_pointer++;

else

{

if (end_word >= (this.query_length - 1))

at_end = 1;

if (end_word != -1)

this.keywords[this.num_words] = (this.query.substring (query_pointer, end_word)).toLowerCase ();

else

{

this.keywords[this.num_words] = this.query.substring (query_pointer, this.query_length);

at_end = 1;

}

this.num_words++;

if (query_pointer != -1)

query_pointer = end_word + 1;

if (query_pointer > (this.query_length - 1))

at_end = 1;

}

}

if (this.num_words == 0)

return (0);

else

{

this.possible_points = this.multiple * this.num_words;

return (1);

}

}

else

return (0);

}



function no_query_found ()

{

document.writeln ('<link rel="stylesheet" href="gp.css">');

document.writeln ('<CENTER><P>Pesquisa em branco.</P></CENTER>');

}

function entry (url, title, keywords, description)

{

this.url = url;

this.title = title;

this.keywords = keywords;

this.description = description;

this.points = 0;

this.search_entry = find_keyword;

this.print_entry = print_result;

}

function find_keyword (the_word)

{

var the_title = this.title.toLowerCase ();

var the_keywords = this.keywords.toLowerCase ();

var the_description = this.description.toLowerCase ();

if ((the_title.indexOf (the_word)) != -1)

this.points += points_title;

if ((the_keywords.indexOf (the_word)) != -1)

this.points += points_keywords;

if ((the_description.indexOf (the_word)) != -1)

this.points += points_description;

}



function print_result (possible_points)

{

document.writeln ('<CENTER><P><A HREF="' + this.url + '" target="meio">' + this.title + '</A>' + this.description + '</P></CENTER>');

}



function no_entry_printed (the_query)

{

document.writeln ('<link rel="stylesheet" href="gp.css">');

document.writeln ("<CENTER><P> Não há Ocorrências para <U><B>" + the_query + "</B></U>.</P></CENTER>"); 

}



function print_intro (the_query)

{

document.writeln ("<CENTER><P>Pesquisando por <U><B>" + the_query + "</B></U>:</P></CENTER>"); 

}

function begin_search ()

{

var key_list = new kw_list;

var entry_printed = 0;

if (! key_list.get_words ())

key_list.no_query ();

else

{

var counter = 0;

var counter2 = 0;

for (counter = 0; counter < entry_num; counter++)

for (counter2 = 0; counter2 <= (key_list.num_words - 1); counter2++)

the_entries[counter].search_entry (key_list.keywords[counter2]);

for (counter = key_list.possible_points; counter > 0; counter--)

{                                                                        

for (counter2 = 0; counter2 < entry_num; counter2++)

{

if (counter == the_entries[counter2].points)

{

if (entry_printed != 1)

{

entry_printed = 1;

print_intro (key_list.original_query);

}

the_entries[counter2].print_entry (key_list.possible_points);

}

}

}

if (! entry_printed)

no_entry_printed (key_list.original_query);

}

}

the_entries = new Array ();

The other function is this:

function retira_acentos(str) 
{

    com_acento = "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐ
ÑÒÓÔÕÖØÙÚÛÜ
ÝŔÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿŕ";

sem_acento = "AAAAAAACEEEEIIIIDN
OOOOOOUUUUYRsBaaa
aaaaceeeeiiiionoooooouuuuybyr";
    novastr="";
    for(i=0; i<str.length; i++) {
        troca=false;
        for (a=0; a<com_acento.length; a++) {
            if (str.substr(i,1)==com_acento.substr(a,1)) {
                novastr+=sem_acento.substr(a,1);
                troca=true;
                break;
            }
        }
        if (troca==false) {
            novastr+=str.substr(i,1);
        }
    }
    return novastr;
}       
  • Look at this other question I marked as duplicate. Although she is asking for something a little different, 3 of the 6 answers there address the question of removing accents, including the my answer.

  • @Victorstafusa, I looked, the question is not the function, but only where I should call it, the function already have and works, I just want to know where I should put in the code, sorry I do not understand anything Javascript, rsrs

  • I removed my vote as a duplicate.

4 answers

45

Simple and easy one-line conversion:

string.normalize('NFD').replace(/[\u0300-\u036f]/g, "");
  • 2

    There yes! Solved in a very practical way!

13

Use this function:

function removeAcento (text)
{       
    text = text.toLowerCase();                                                         
    text = text.replace(new RegExp('[ÁÀÂÃ]','gi'), 'a');
    text = text.replace(new RegExp('[ÉÈÊ]','gi'), 'e');
    text = text.replace(new RegExp('[ÍÌÎ]','gi'), 'i');
    text = text.replace(new RegExp('[ÓÒÔÕ]','gi'), 'o');
    text = text.replace(new RegExp('[ÚÙÛ]','gi'), 'u');
    text = text.replace(new RegExp('[Ç]','gi'), 'c');
    return text;                 
}
  • 1

    I already have the full function, I would just like to know the syntax of two functions together, for example, how to add another function, in this line for example: var the_keywords = this.keywords.toLowerCase (); , where do I put this other function? Thank you for your reply

  • Isn’t it just calling one function after another? Or joining them in one?

  • Whatever, just don’t know where and how to call the function removeAcento along with toLowerCase, get it? Thanks for your attention

  • I made an edit on my function, which now lowercase and removes the accents. Just call it with the content between relatives.

  • I’ll test it, thank you

  • Would it look like this? for example: this.query = this.original_query.removeAcento (text);

  • No... removeAcento(string); Instead of string, put the name of your variable or text in quotes.

  • var nova_variable = removeAcento(variable);

  • It worked perfectly Lucaswmolin, thank you very much!

  • the script to remove accent that passed me, is working well in firefox, but in other browsers does not work well, know to say Porq?

  • In principle, it should work normally in any browser. The problem is still occurring?

Show 6 more comments

13


It would be enough to replace that:

var the_title = this.title.toLowerCase();

That’s why:

var the_title = retira_acentos(this.title.toLowerCase());

I mean, just call the function retira_acentos giving you the string from which the accents should be removed.

I just note that the convention/standardization of names in javascript dictates to use camelCase. I mean, instead of using the_title, the_keywords, retira_acentos, etc, the names would be theTitle, theKeywords, retiraAcentos, etc..

4

Use a slugify

export const convertToSlug = (text) => {
  const a = 'àáäâãèéëêìíïîòóöôùúüûñçßÿœæŕśńṕẃǵǹḿǘẍźḧ·/_,:;'
  const b = 'aaaaaeeeeiiiioooouuuuncsyoarsnpwgnmuxzh------'
  const p = new RegExp(a.split('').join('|'), 'g')
  return text.toString().toLowerCase().trim()
    .replace(p, c => b.charAt(a.indexOf(c))) // Replace special chars
    .replace(/&/g, '-and-') // Replace & with 'and'
    .replace(/[\s\W-]+/g, '-') // Replace spaces, non-word characters and dashes with a single dash (-)
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.