• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

172
Views
Función javascript de cadena limpia

Este es mi escenario real

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

Ahora tengo que agregar otro caso cuando una ciudad contiene la cadena "%c3%9f" y quiero reemplazarla con 's'

¿Cómo puedo agregarlo en mi función actual?

about 3 years ago · Santiago Gelvez
2 answers
Answer question

0

En primer lugar, debe tener en cuenta que la función String.replaceAll() no es compatible con IE.

Si está de acuerdo con eso y supongo que lo está porque ya lo está usando, entonces simplemente crearía una matriz de pares que puede distribuir en la función replaceAll .

Algo como esto:

 const replaceMap = [ ['%20', ' '], ['%c3%9f', 's'], ]; const cleanString = (string) => { let city = getAccentedCity(string); replaceMap.forEach(pair => city = city.replaceAll(...pair)); return city; }
about 3 years ago · Santiago Gelvez Report

0

Simplemente agregue más pruebas si no desea volver a escribir el código

 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 3 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error