Question

How would I go on about changing éëíïñÑ (etc) to their counterparts? ie, eeiinN.

I was thinking about doing regex matching against é -> é and replacing both & and acute/grave; with empty strings, but I can't seem to find an AS3 function that encodes accents to their non-numerical entities (ê and the like). I've already tried using an associative array, a la entities["À"] = "A";, but AS3 seems to dislike the unicode keys.

Any suggestions would be greatly appreciated.

Thanks!

Was it helpful?

Solution

This is called "unicode decomposition", so you may want to Google for that. However, if you are dealing with languages other than your own, don't do this.

I know the idea seems reasonable to native English speakers who know no other languages, but to people for whom those characters are letters it makes as much sense as replacing "W" with "VV", "d" with "cl" and "Q" with "O," would to an English speaker.

P.S. Since you asked:

You could loop through the string doing charCodeAt() and do your associative array on the integers. But I still don't recommend it.

"Doña" means "lady" but "dona" means "doughnut". "De" means "from" and "dé" means "give". And so on and so forth.

They aren't just normal letters with annoying flyspecks, they are actually as distinct as "E" and "F" or "P" and "R".

OTHER TIPS

Thank You for the response.

I'm from Argentina, we speak Spanish, and like MarkusQ said, removing an accent can change the entire meaning of a word. But I would also like to say that there are some scenarios where removing the accents is a better.

I'm writing a script for a search function, where you can find people by its name. The data comes from a registration form where people write it's names the way they like, ie: Lopez/Lópes, Matias/Matías, etc.

So for AS3 "Lopez" and "López" are distinct words and I would like to have them both in the search results when an user searches for "lopez".

That way, my solution is to remove all the accents, and use a mask in the search box's textfield to avoid the use of accents. I think is the best solution, anyone has a better idea? Thank You!

Fixed this for german, as you use "Ae" for "Ä", "Oe" for "Ö" etc.

    /**
     * Helper arrays for unicode decomposition
     */
     var pattern:Array = new Array();
        pattern.push(new RegExp("Š", "g") );
        pattern.push( new RegExp("[΅]", "g"));
        pattern.push( new RegExp("Ž", "g"));
        pattern.push( new RegExp("š", "g"));
        pattern.push(new RegExp("[Ϛ]", "g"));
        pattern.push( new RegExp("ž", "g"));
        pattern.push(new RegExp("[ÀÁÂÃÅ]","g"));
        pattern.push( new RegExp("[ÆÄ]","g"));
        pattern.push( new RegExp("Ç","g"));
        pattern.push( new RegExp("[ÈÉÊË]","g"));
        pattern.push(new RegExp("[ÌÍÎÏ]", "g"));
        pattern.push( new RegExp("Ð", "g"));
        pattern.push( new RegExp("Ñ","g"));
        pattern.push( new RegExp("[ÒÓÔÕØ]", "g"));
        pattern.push( new RegExp("Ü","g"));
        pattern.push( new RegExp("[ÙÚÛ]","g"));
        pattern.push( new RegExp("[ŸÝ]", "g"));
        pattern.push( new RegExp("Þ", "g"));
        pattern.push( new RegExp("ß", "g"));
        pattern.push( new RegExp("[àáâãå]","g"));       
        pattern.push( new RegExp("[æä]","g"));
        pattern.push( new RegExp("ç","g"));
        pattern.push( new RegExp("[èéêë]","g"));
        pattern.push( new RegExp("[ìíîï]","g"));
        pattern.push( new RegExp("ð", "g"));
        pattern.push( new RegExp("ñ","g"));
        pattern.push( new RegExp("[òóôõø]", "g"));
        pattern.push( new RegExp("ü","g"));
        pattern.push( new RegExp("[ùúû]","g"));
        pattern.push( new RegExp("[ýÿ]","g"));
        pattern.push( new RegExp("þ", "g"));

         var patternReplace:Array = [
            "S",
            "Oe",
            "Z",
            "s",
            "oe",
            "z",
            "A",
            "Ae",
            "C",
            "E",
            "I",
            "D",
            "N",
            "O",
            "Ue",
            "U",
            "Y",
            "Th",
            "ss",
            "a",
            "ae",
            "c",
            "e",
            "i",
            "d",
            "n",
            "o",
            "ue",
            "u",
            "y",
            "th"];

    /**
     * Returns the Unicode decomposition of a given run of accented text. 
     * @param value The original string
     * @return The string without accents
     */     
    private static function decomposeUnicode(str:String):String
    {
        for (var i:int = 0; i < pattern.length; i++)
        {
            str = str.replace(pattern[i], patternReplace[i]);
        }
        return str;
    }
    private var sdiakA:Array;
    private var bdiakA:Array;
    private function initReplaceDiacritic(){
        var sdiak = "áäčďéěíĺľňóôöŕšťúůüýřžÁÄČĎÉĚÍĹĽŇÓÔÖŔŠŤÚŮÜÝŘŽ";
        var bdiak = "aacdeeillnooorstuuuyrzAACDEEILLNOOORSTUUUYRZ";
        sdiakA = new Array();
        bdiakA = new Array();

        for (var i=0;i<sdiak.length;i++)
            sdiakA.push(new RegExp(sdiak.charAt(i), "g"))
        for (i=0;i<sdiak.length;i++)
            bdiakA.push(bdiak.charAt(i))
    }
    private function replaceDiacritic(string:String){               
        for (var i:int = 0; i < sdiakA.length; i++)
            string = string.replace(sdiakA[i], bdiakA[i]);
        return (string)
    }
initReplaceDiacritic();
var str = replaceDiacritic("šžřáíéééíčšřčš");

I hope this will be useful for anybody: https://github.com/infralabs/DiacriticsRemove

This class removes diacritics from strings containing Latin-1 Supplement, Latin Extended-A and Latin Extended-B special characters.

usage:

var specialCharacters:String = "";
specialCharacters+="Latin-1 Supplement\n";
specialCharacters+="ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ\n";
specialCharacters+="Latin Extended-A\n";
specialCharacters+="ĀāĂ㥹ĆćĈĉĊċČčĎďĐđĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħĨĩĪīĬĭĮįİıIJijĴĵĶķĸĹĺĻļĽľĿŀŁłŃńŅņŇňʼnŊŋŌōŎŏŐőŒœŔŕŖŗŘřŚśŜŝŞşŠšŢţŤťŦŧŨũŪūŬŭŮůŰűŲųŴŵŶŷŸŹźŻżŽžſ\n";
specialCharacters+="Latin Extended-B\n";
specialCharacters+="ƒǺǻǼǽǾǿ\n";
specialCharacters+="Latin Extended Additional\n";
specialCharacters+="ẀẁẂẃẄẅỲỳ\n";

trace(new DiacriticsRemove().Parse(specialCharacters));

source:

Latin-1 Supplement

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

Latin Extended-A

ĀāĂ㥹ĆćĈĉĊċČčĎďĐđĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħĨĩĪīĬĭĮįİıIJijĴĵĶķĸĹĺĻļĽľĿŀŁłŃńŅņŇňʼnŊŋŌōŎŏŐőŒœŔŕŖŗŘřŚśŜŝŞşŠšŢţŤťŦŧŨũŪūŬŭŮůŰűŲųŴŵŶŷŸŹźŻżŽžſ

Latin Extended-B

ƒǺǻǼǽǾǿ

Latin Extended Additional

ẀẁẂẃẄẅỲỳ

result:

Latin-1 Supplement

AAAAAAAECEEEEIIIIDNOOOOO×OUUUUYTHssaaaaaaaeceeeeiiiidnooooo÷ouuuuythy

Latin Extended-A

AaAaAaCcCcCcCcDdDdEeEeEeEeEeGgGgGgGgHhHhIiIiIiIiIiIJijJjKkĸLlLlLlLlLlNnNnNnnNnOoOoOoOEoeRrRrRrSsSsSsSsTtTtTtUuUuUuUuUuUuWwYyYZzZzZzs

Latin Extended-B

fAaAEaeOo

Latin Extended Additional

WwWwWwYy

Another alternative:

    public static function noAccent(source:String):String
    {
        source = source.replace(/[àáâãäå]/g, "a");
        source = source.replace(/[ÀÁÂÃÄÅ]/g, "A");
        source = source.replace(/[èéêë]/g, "e");
        source = source.replace(/[ËÉÊÈ]/g, "E");
        source = source.replace(/[ìíîï]/g, "i");
        source = source.replace(/[ÌÍÎÏ]/g, "I");
        source = source.replace(/[ðòóôõöø]/g, "o");
        source = source.replace(/[ÐÒÓÔÕÖØ]/g, "O");
        source = source.replace(/[ùúûü]/g, "u");
        source = source.replace(/[ÙÚÛÜ]/g, "U");
        source = source.replace(/[ýýÿ]/g, "y");
        source = source.replace(/[ÝÝŸ]/g, "Y");
        source = source.replace(/[ç]/g, "c");
        source = source.replace(/[Ç]/g, "C");
        source = source.replace(/[ñ]/g, "n");
        source = source.replace(/[Ñ]/g, "N");
        source = source.replace(/[š]/g, "s");
        source = source.replace(/[Š]/g, "S");
        source = source.replace(/[ž]/g, "z");
        source = source.replace(/[Ž]/g, "Z");
        source = source.replace(/[æ]/g, "ae");
        source = source.replace(/[Æ]/g, "AE");
        source = source.replace(/[œ]/g, "oe");
        source = source.replace(/[Œ]/g, "OE");
        return source;
    }

Source: http://blog.geturl.net/post/2010/01/31/[AS3]-Supprimer-tous-les-accents-d-une-chaine-de-caract%C3%A8re

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top