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

207
Vistas
Clean string javascript function

This my actual scenario

const cleanString=( string )=>{
  let city = getAccentedCity(string);
  if( city.indexOf('%20')<0 ) return city;
  return city.replaceAll('%20', ' ');
  
}

I have now to add an other case when a city contains the string "%c3%9f" and i want to replace it with 's'

How can i add it in my current function?

about 4 years ago · Santiago Gelvez
2 Respuestas
Responde la pregunta

0

First of all you should be aware that String.replaceAll() function is not supported on IE.

If you're ok with it and I assume you are since you're already using it, then I would just create an array of pairs that you can spread into the replaceAll function.

Something like this:

const replaceMap = [
  ['%20', ' '],
  ['%c3%9f', 's'],
];

const cleanString = (string) => {
  let city = getAccentedCity(string);
  replaceMap.forEach(pair => city = city.replaceAll(...pair));
  return city;
}
about 4 years ago · Santiago Gelvez Denunciar

0

Just add more tests if you do not want to rewrite the code

const cleanString= string => {
  let city = getAccentedCity(string);
  if (city.toLowerCase().indexOf('%c3%9f') >= 0 ) return city.replaceAll('%c3%9f', 's');
  if (city.indexOf('%20') < 0) return city;
  return city.replaceAll('%20', ' ');
}
about 4 years ago · Santiago Gelvez 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