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

148
Vistas
Javascript Capitalize first letter of each word ignore Contractions

I am trying to Capitalize the first letter of each word in a string. I found similar questions online but none seem to answer my question of ignoring Contractions like can't, won't, wasn't.

This snippet of code works but it also capitalizes the letter after the apostrophe in the contraction.

var str = str.replace(/\b\w/g, w => w.toUpperCase())

If the string contains a contraction like can't or won't it will output Can'T or Won'T.

Is there a way to ignore apostrophes that are in the middle of a word? I still want to capitalize words that are separated by other punctuation. For example:

  • this_is_an_example -> This_Is_An_Example
  • this/is/an/example -> This/Is/An/Example
  • this,is,an,example -> This,Is,An,Example
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can use

const texts = ["this_can't_be_an_example", 'this/is/an/example', 'this,is,an,example']
for (const text of texts) {
  console.log(text, '=>', text.replace(/([\W_]|^)(\w)(?<!\w'\w)/g, (_, x,y) => `${x}${y.toUpperCase()}` ))
}

See the regex demo. Details:

  • ([\W_]|^) - Group 1 (x): a non-alphanumeric char or start of string
  • (\w) - Group 2 (y): a word char
  • (?<!\w'\w) - a negative lookbehind that makes sure the Group 2 value is not preceded with a word char and a '.
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use a negative lookbehind to make sure that there isn't an apostrophy before the word boundary. (?:\b|(?<=_)) checks for a word boundary or a underscore before the word.

const examples = [
  "can't",
  "this-is-an-example",
  "this.is.an.example",
  "this/is/an/example",
  "this_is_an_example",
  "this,is,an,example",
];

examples.forEach(example => {
  console.log(example.replace(/(?<!')(?:\b|(?<=_))\w/g, w => w.toUpperCase()));
});

about 4 years ago · Juan Pablo Isaza Denunciar

0

This RegExp detect

  • First symbol
  • Underline
  • Space
  • / symbol
  • Comma
  • Apostrophe

Just add need symbols in list with | separator

str.replace(/((^|_| |\/|,|')\w)/g, w => w.toUpperCase());
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