Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

164
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda