Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

190
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!