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

118
Visualizações
Regex for hashtags tree takes too long to execute

In our app we have topics which user can create, and each topic must have it's own hashtag (or hashtags hierarchy). We have this kinda Regex for validation:

const REGEX_HASHTAG = /^(#[w]?((\/?)([a-z0-9]+)+)+)(,\s{0,1}#[a-z0-9]?((\/?)([a-z0-9]+)+)+)*$/g;

What i need is for user to be able to create hashtags which have structure like this:

  1. (#) symbol
  2. Text in lowercase
  3. Optional slash (/) followed by lowercase text to create hierarchy

And also users can put comma (and optional whitespace) followed by new hashtag or hashtag hierarchy. When i put too many letters and slash at the end Regex stops working, takes too long to execute. What am i doing wrong?

regexr.com/6hpqo

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There are quite a few nested quantifiers and optional parts, which can cause catastrophic backtracking when there is no match.

You could write the pattern as

^#[a-z0-9]+(?:\/[a-z0-9]+)*(?:,\s*#[a-z0-9]+(?:\/[a-z0-9]+)*)*$
  • ^ Start of string
  • #[a-z0-9]+ Match # and 1+ repetitions of the listed characters in the character class
  • (?:\/[a-z0-9]+)* Optionally repeat the / and the same character class
  • (?: Non capture group
    • ,\s* Match a comma and optional whitespace chars
    • #[a-z0-9]+(?:\/[a-z0-9]+)* The same pattern as in the first part
  • )* Close the non capture group and optionally repeat it
  • $ End of string

Regex demo

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