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

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

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 Report

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 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!