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

192
Vistas
Find and detect a pattern in a string

I'm looking for a function that detects repeated patterns on a string, for example, if the input is:

var string = "HelloHelloHelloHelloHelloHello";
var string2 = "Hello this is a repeated pattern Hello this is a repeated pattern Hello this is a repeated pattern Hello this is a repeated pattern";

it should return true as there are many times repeated "Hello" or "Hello this is a repeated pattern"

I have tried something like this:

function detectPattern(string) {
    string = string.replace(/\s+/g,"_");
    return /(\S)(\1{15,})/g.test(string);
}

but that will only detect if the text contains the same character many times.

Any idea or suggestion?

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

0

A regex that would work is the following:

(.*?)( ?\1)+

This will match:

  • (.*?): the least amount of characters, followed by
  • ( ?\1)+: an optional space and the very same characters of the first group, multiple times

In order to extract your repeated pattern, it's sufficient to extract the content of Group 1.

Check the demo here.

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