javascript - replacing special characters with other character from array in js - Stack Overflow

I would like to get rid of special characters in a string by paring each of it's character to a ch

I would like to get rid of special characters in a string by paring 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

I would like to get rid of special characters in a string by paring 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

Share Improve this question edited May 14, 2013 at 0:41 Bart Az. asked May 14, 2013 at 0:39 Bart Az.Bart Az. 1,6451 gold badge22 silver badges32 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 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.

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. :-)

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"

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1741423472a4346233.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信