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

530
Vistas
How to use Regex to capitalize a street address?

In my application, you can submit a street address, and in the front end I convert it all to lowercase to be stored in the database. Now when I fetch the data I get an address that looks like: "1 pleasant valley drive"

Can I make a regex to capitalize the first letter of each word in the string?

End goal: "1 Pleasant Valley Dr"

I'm currently using:

let addrFormat =
    address?.split(" ")[0] +" " +
    address?.split(" ")[1].charAt(0).toUpperCase() +
    address?.split(" ")[1].substring(1) +
    " " + address?.split(" ")[2].charAt(0).toUpperCase() +
    address?.split(" ")[2].substring(1);

but I need it to scale. lets say the street address is:

1234 Rocky Mountain Road

Then I have a problem with my code because it wont capitalize the last word.

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

0

You can simply uppercase the letters that aren't proceeded by a word character.
This can be checked with a word-boundary \b

let address = "1 pleasant valley drive";

address = address.replace(/\b(\w)/g, (m,g) => g.toUpperCase())

console.log(address);

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