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

100
Vistas
How to title-case a string with some excluded word exceptions and how to collapse/trim additional white space sequences as well?

I haven't done a lot of work with regex, and I'm getting stuck. I'm trying to take a string and make it title case, but with some exceptions. I also want to remove any whitespace.

Currently it's removing whitespace and the title case is working, but it's not following the exceptions. Is there a way to combine the "title" variable with "regex" variable, and make it so the exceptions are working?

const toTitleCase = str => {
  const title = str.replace(/\s\s+/g, ' ');
  const regex = /(^|\b(?!(AC | HVAC)\b))\w+/g;
  const updatedTitle = title
    .toLowerCase()
    .replace(regex, (s) => s[0].toUpperCase() + s.slice(1));

  return updatedTitle;
}

console.log(toTitleCase(`this is an HVAC AC converter`))

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

0

From the above comment ...

"It looks like the OP wants to exclude the words 'AC' and 'HVAC' from the title-case replacement. A pattern which would achieve this is e.g. \b(?!HVAC|AC)(?<upper>[\w])(?<lower>[\w]+)\b"

The code which covers all of the OP's requirements then might look like the following example ...

function toTitleCase(value) {
  return String(value)
    .trim()
    .replace(/\s+/g, ' ')
    .replace(
      /\b(?!HVAC|AC)(?<upper>[\w])(?<lower>[\w]+)\b/g,
      (match, upper, lower) => `${ upper.toUpperCase() }${ lower.toLowerCase() }`,
    );
}

console.log(
  "toTitleCase('  This  is an HVAC   AC converter. ') ...",
  `'${ toTitleCase('  This  is an HVAC   AC converter. ') }'`
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

One could take the above approach a step further by providing the to be excluded words as additional parameter(s) ...

function toTitleCase(value, ...exludedWordList) {
  const exceptions = exludedWordList
    .flat(Infinity)
    .map(item => String(item).trim())
    .join('|');
  return String(value)
    .trim()
    .replace(/\s+/g, ' ')
    .replace(
      RegExp(`\\b(?!${ exceptions })(?<upper>[\\w])(?<lower>[\\w]+)\\b`, 'g'),
      (match, upper, lower) => `${ upper.toUpperCase() }${ lower.toLowerCase() }`,
    );
}

console.log(
  "toTitleCase('  this  is an HVAC   AC converter. ', ['AC', 'HVAC']) ...",
  `'${ toTitleCase('  this  is an HVAC   AC converter. ', ['AC', 'HVAC']) }'`
);
console.log(
  "toTitleCase('  this  is an HVAC   AC converter. ', 'is', 'an', 'converter') ...",
  `'${ toTitleCase('  this  is an HVAC   AC converter. ', 'is', 'an', 'converter') }'`
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

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