I have the function for translate cirrilic characters to latin, and also some characters. Also, I need to translate '.' to '-', but if I add '.':'-'
to the ru-object, the numbers are translated as undefined
. Escaping a point \.
don't help.
I can add in ru-object each number to fix, but I want to understand, why this is happening.
let ru = {
'а': 'a', 'б': 'b', 'в': 'v', 'г': 'g', 'д': 'd',
'е': 'e', 'ё': 'e', 'ж': 'j', 'з': 'z', 'и': 'i', 'й': 'j',
'к': 'k', 'л': 'l', 'м': 'm', 'н': 'n', 'о': 'o',
'п': 'p', 'р': 'r', 'с': 's', 'т': 't', 'у': 'u',
'ф': 'f', 'х': 'h', 'ц': 'c', 'ч': 'ch', 'ш': 'sh',
'щ': 'shch', 'ъ': '', 'ы': 'y', 'ь': '',
'э': 'e', 'ю': 'u', 'я': 'ya', ' ': '-', '/': '-', '«':'', '»':'', '.':'-'
};
function trans(str){
str1 = str.toLowerCase();
let result = str1.replace(
new RegExp(Object.keys(ru).join('|'), 'g'),
function(match) {
return ru[match];
}
);
return result;
}
alert( trans('150собственников.ру')); //undefinedundefinedundefinedsobstvennikov-ru