how to replace all accented characters with English equivalents

5k views Asked by At

Hi in my aura component below code is used to replace all accented characters with English equivalents but I have update aura component version after which String.prototype function is no longer supported please help with its alternative how to replace all accented characters

var input = component.get('v.newSalesOrder.Invoice_Message__c');
    
  /*  var languageMap = component.get("v.languageMap");
    
    String.prototype.portuguese=function(){
        return this.replace(/[^A-Za-z0-9\[\] ]/g,
                            function(a){
                                return languageMap.portuguese_map[a]||a}
                           )
    };
    component.set('v.newSalesOrder.Invoice_Message__c', input.portugueze());  

var languageMap = component.get("v.languageMap");
    
    languageMap.portuguese_map={
        "Á":"A", "á":"a", "Â":"A", "â":"a", "À":"A", "à":"a", "Å":"A", "å":"a", "Ã":"A", "ã":"a",
        "Ä":"A", "ä":"a", "Æ":" ", "æ":" ", "É":"E", "é":"e", "Ê":"E", "ê":"e", "È":"E", "è":"e",
        "Ë":"E", "ë":"e", "Ð":" ", "ð":" ", "Í":"I", "í":"i", "Î":"I", "î":"i", "Ì":"I", "ì":"i",
        "Ï":"I", "ï":"i", "Ó":"O", "ó":"o", "Ô":"O", "ô":"o", "Ò":"O", "ò":"o", "Ø":" ", "ø":" ",
        "Õ":"O", "õ":"o", "Ö":"O", "ö":"o", "Ú":"U", "ú":"u", "Û":"U", "û":"u", "Ù":"U", "ù":"u",
        "Ü":"U", "ü":"u", "Ç":"C", "ç":"c", "Ñ":"N", "ñ":"n", "Ý":"Y", "ý":"y", "\"":" ", "<":" ",
        ">":" ", "&":" ", "®":" ", "©":" ", "Þ":" ", "þ":" ", "ß":" ", "=":" "
    };        
    component.set("v.languageMap",languageMap);
},
2

There are 2 answers

1
picocode On BEST ANSWER
function Convert(string){
  return string.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
}
console.log(Convert("Ë À Ì Â Í Ã Î Ä Ï Ç Ò È Ó É Ô Ê Õ Ö ê Ù ë Ú î Û ï Ü ô Ý õ â "))

Output:

"E A I A I A I A I C O E O E O E O O e U e U i U i U o Y o a "
0
PatricNox On

I had the same need for a project and found myself creating a package for it. https://github.com/PatricNox/SpecialToNormal

import normalizeSpecialCharacters from "specialtonormal";


var input = component.get('v.newSalesOrder.Invoice_Message__c');
component.set('v.newSalesOrder.Invoice_Message__c', normalizeSpecialCharacters(input.portugueze()));