1

I would like to get rid of special characters in a string by comparing each of it's character to a character in an array and replacing it with a matching one. The function below does not throw any errors but keeps returning the string unchanged

    var name = "przykład";      

    // the characters i'm looking for in a string:
    var charList = ["Ą","ą","Ć","ć","Ę","ę","Ł","ł","Ó","ó","Ś","ś","Ź","ź","Ż","ź"];

    // the characters i'd like to replace them with:
    var replaceList = ["A","a","C","c","E","e","L","l","O","o","S","s","Z","z","Z","z"];

    var limit = name.length;
    for (i = 0; i < limit; i++){
        for(var j in charList){
            name.charAt(i) === charList[j] ? name.replace(name.charAt(i), replaceList[j]) : "";
        }
    }

    return name;

I know this question will be most likely closed as "too localized" and it's propably a stupid and easy mistake i've made but still I would really appreciate any help with this

3 Answers 3

5

Usually, the result of the replace function is returned as a new String object in most of the programming languages. You should change your code to this:

if (name.charAt(i) === charList[j])
    name = name.replace(name.charAt(i), replaceList[j]);

Also, since the replace function will replace all the occurrences of that character, you could change your algorithm a little bit.

Sign up to request clarification or add additional context in comments.

2 Comments

Replace in javascript returns a string primitive, not an object (which might be inferred from your answer).
@RobG Thank you for clarification, although I meant (as I explicitly said) "in most of the programming languages". I wrote object since in most languages classes and types are implemented as objects.
5

You can put the mapping into an object, which has the advantage of being a bit easier to maintain since the character and its replacement are adjacent in the object, rather than trying to align position in an array.

e.g.

var name = "przykłąd Ęś";

// Object of characters to replace and their replacement values
var charList = {'Ą':'A', 'ą':'a', 'Ć':'C', 'ć':'c', 'Ę':'E', 'ę':'e',
                'Ł':'L', 'ł':'l', 'Ó':'O', 'ó':'o', 'Ś':'S', 'ś':'s',
                'Ź':'Z', 'ź':'z', 'Ż':'Z', 'ż':'z'};

// For each character in the string, search for it in charList and if found,
// replace it with the value
alert(
  name + '\n' + name.replace(/./g, function(c) {return c in charList? charList[c] : c})
);

There is likely something cleverer that can be done with char codes, but I can't think of it right now.

Edit 2017

Fixed last character mapping—thanks @MarekSkiba. :-)

1 Comment

You have duplicated property ź in charList , the last property should be 'ż':'z'.
1

Here my solution...

var cleanName = function(str) {
    if ($.trim(str) == '') return str; // jQuery
    str = $.trim(str).toLowerCase();
    var special = ['&', 'O', 'Z', '-', 'o', 'z', 'Y', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', '.', ' ', '+', '\''],
        normal = ['et', 'o', 'z', '-', 'o', 'z', 'y', 'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'd', 'n', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'o', 'n', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', '_', '_', '-', '-'];
    for (var i = 0; i < str.length; i++) {
        for (var j = 0; j < special.length; j++) {
            if (str[i] == special[j]) {
                str = str.replace(new RegExp(str[i], 'gi'), normal[j]);
            }
        }
    }
    str = str.replace(/[^a-z0-9_\-]/gi, '_');
    str = str.replace(/[\-]{2,}/gi, '_');
    str = str.replace(/[\_]{2,}/gi, '_');
    return str;
};

console.log(cleanName('l\'éléphant')); // "l-elephant"

Comments

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.