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

166
Visualizações
Trying to match multiple of a word in one string

I have a command for a bot I work on that's as follows :

.command @user @user2 (user list can grow etc..) item

The current regex I am using is

/([.command]+ [@A-Za-z0-9_?]+ [A-Za-z0-9]+)/g

The problem I have is that I need it to find each user instead of only the first occurrence. Ideally I'd also like to be able to target that each user starts with a "@" character.

Any help on this appreciated.

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

0

Use

/^\.([a-z]+)\s+((?:@[\w?]+\s+)+)(\w+)/i

Capture group 1 is the command.

Capture group 2 will contain all the @user parameters, you can use .split(/\s+/) to split it into separate users.

Capture group 3 is the item at the end

about 4 years ago · Juan Pablo Isaza Relatório

0

This is one (of many) solution:

function getUsers(command){

   var result = [];

   var index = command.indexOf("@");
   while(index>-1){
      var i1 = command.indexOf("@", index);
      var i2 = command.indexOf(" ", i1);
      if(i2==-1){i2 = command.length;}
      var user = command.substr(i1, i2-i1);
      index = command.indexOf("@", i2);
      result.push(user);
   }

   return result;

}

getUsers("cmd @a1 @a2 @a3 ckkasd @a5 a6 @a7")

This one dont uses regular expressions. It will consume less memory and will run faster for big strings.

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