Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

188
Vistas
Regexp, if I add the point to the object for replace the string, the numbers are translated as as undefined

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

https://jsfiddle.net/Nata_Hamster/49bnLmqd/

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You need to keep the ru array as is, but when building the regex from keys, escape the dot.

You can use

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) {
  const regex = new RegExp(Object.keys(ru).map(x => x.replace('.', '\\.')).join('|'), 'g');
  let result = str.toLowerCase().replace(regex, function(match) {
    return ru[match[0]];
  });
  return result;
}

console.log(trans('150собственников.ру'));

If you want to use any chars, make sure you simply use the right escaping function, see Is there a RegExp.escape function in JavaScript?. Then, you will just need to replace .map(x=>x.replace('.','\\.')) with .map(x=>x.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')).

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda