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

197
Visualizações
Regex - Detect all *separated* usernames in a string

I am trying to detect all usernames following this form:

/@[a-zA-Z0-9_.-]{3,30}/

For example, in the following string

@eminem is singing with @rihanna

, the method must return

["@eminem", "@rihanna"]

I have tried the following:

function matchAllUsernames(str) {
  const pattern = /@[a-zA-Z0-9_.-]{3,30}/g;
  return [...str.matchAll(pattern)].flat();
}

const str = "@eminem is singing with @rihanna";

console.log(matchAllUsernames(str));

But... what if I try with this string?

@eminem@ndsoiosd is singing with @rihanna

In this case, the method must ignore @eminem and @ndsoiosd, because there is no sparation between them...

function matchAllUsernames(str) {
  const pattern = /@[a-zA-Z0-9_.-]{3,30}/g;
  return [...str.matchAll(pattern)].flat();
}

const str = "@eminem@ndodshuoc is singing with @rihanna";

console.log(matchAllUsernames(str));

How can I do that?

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

0

You can prepend an anchor for a non word boundary before the @ and assert a whitespace boundary to the right at the end of the pattern:

\B@[a-zA-Z0-9_.-]{3,30}(?!\S)

Regex demo

function matchAllUsernames(str) {
  const pattern = /\B@[a-zA-Z0-9_.-]{3,30}(?!\S)/g;
  return [...str.matchAll(pattern)].flat();
}

const str = "@eminem@ndodshuoc is singing with @rihanna";

console.log(matchAllUsernames(str));

To support also the characters . and - not being allowed before the next @ sign:

(?<!\S)@[a-zA-Z0-9_.-]{3,30}(?!\S)

Regex demo

about 4 years ago · Juan Pablo Isaza Relatório

0

You can match consecutive mentions and discard those matches and only extract mentions from other contexts:

function matchAllUsernames(str) {
  const pattern = /(?:@[\w.-]{3,30}){2,}|(@[\w.-]{3,30})/g;
  return Array.from(str.matchAll(pattern), x=>x[1]).filter(Boolean);
}

const str = "@sia@ndodshuoc and @eminem are singing with @rihanna";
console.log(matchAllUsernames(str));

Note that [A-Za-z0-9_] is equal to \w.

The (?:@[\w.-]{3,30}){2,}|(@[\w.-]{3,30}) regex matches two or more sequences of 3 to 30 word, . or - chars, or captures into Group 1 a sequence of 3 to 30 word, . or - chars (in another context).

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