Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

196
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda