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

218
Vistas
Javascript how to replace the fourth character with another character?, not divide, but replace

How to change a character every n number of characters?

I have a text string,

let string = 'abcdefghijklm'

I want to change every 4 characters the character for a '_'

to get this:

abc_efg_ijk_m

this works for me, but it splits it with '_' and it doesn't exactly replace the fourth character

const addChar = (stringer, charac, number) => {
    let strinChar = "";
    const long = stringer.length;
    for (let i = 0; i < long; i += number) {
        if (i + number < long) {
            strinChar += stringer.substring(i, i + number) + charac;
        } else {
            strinChar += stringer.substring(i, long);
        }
    }
    return strinChar;
}
console.log(addChar(string, '_', 4))

output:

abcd_efgh_ijkl_m

and I want this output:

abc_efg_ijk_m

a similar output is this but in the same way I do not get the desired result

string0.match(/.{1,3}/g).join('_');

I have also tried:

let kool = string.match(/.{1,4}/g)
let newArray = []
for(let i=0; i<=kool.length-1; i++){
  newArray.push(kool[i].slice(0, -1))
  }
console.log(newArray.join('_'));

but I don't get the desired result

about 4 years ago · Juan Pablo Isaza
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