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

147
Vistas
Regex add space in string if the word is longer than 4 characters and have numbers

I try to create a regex with 2 condition:

  • if word length more than 4 character
  • And if the word contains numbers

I need to add spaces

So like: iph12 return iph12, but iphone12 return iphone 12

I wrote regex

.replace(/\d+/gi, ' $& ').trim()

and this function return in anyway string like iphone 12. I tried to use function

.replace(/(?=[A-Z]+\d|\d+[A-Z])[A-Z\d]{,4}/i, ' $& ').trim()

but without second argument in {,4} it's not working. So is this possible?

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

0

You can use

text.replace(/\b([a-zA-Z]{4,})(\d+)\b/g, '$1 $2')

See the regex demo. Details:

  • \b - word boundary
  • ([a-zA-Z]{4,}) - Group 1: four or more ASCII letters
  • (\d+) - Group 2: one or more digits
  • \b - word boundary

See the JavaScript demo:

const texts = ['iphone12', 'iph12'];
const regex = /\b([a-zA-Z]{4,})(\d+)\b/g;
for (const text of texts) {
    console.log(text, '=>', text.replace(regex, '$1 $2'));
}

Output:

iphone12 => iphone 12
iph12 => iph12
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