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

175
Views
RegExp matching @ mention tags with optional space

I'm working with some code that uses the regex /(@\w+) ?/g for matching tagged mentions

This will match the following text returning the groups correctly without the additional space

testing @one and @two and @three

If I remove the optional space ? the regex /(@\w+)/g appears to return the same matching groups - for the above example and any other test strings I've tried

Is there some subtle difference or edge case that I might be missing between these two version of the regex?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If there is a space the regex will match it, but it won't affect the matching groups because the space isn't in any group. Only the whole match will have the space.

console.log(...'testing @one and @two and @three'.matchAll(/(@\w+) ?/g));
// [ "@one ", "@one" ]
// [ "@two ", "@two" ]
// [ "@three", "@three" ]

The first element of each result array is the whole match and the second element is what is matched by the group (@\w+).

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!