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

145
Vistas
Is there way to change a substring in js?

I have a string like:

def definition():

I want to change word def (for example), every instance of word def but not the "def"s that are part of other words like this

console.log("def definition():".specialReplace("def", "abc"));

and result should be

abc definition():

not

abc abcinition():
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Use String#replace or String#replaceAll with a regular expression:

const specialReplace = (str) => str.replaceAll(/\bdef\b/g, 'abc')
console.log(specialReplace("def definition")) // abc definition
console.log(specialReplace("def definition def")) // abc definition abc

In the regular expression, \b is a boundary type assertion that matches any word boundary, such as between a letter and a space.

Note that the same sequence \b is also used inside character class regular expression positions ([\b]), to match the backspace character.

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