0

Why I am not getting the three mentions but the first one repeated twice?

console.log(/(@[\w]+)/g.exec('i am mentioning @user1 and @user2 and @user3'))
Jose Ospina
  • 2,097
  • 3
  • 26
  • 40
  • Also, note that `[\w]+` can be shortened to `\w+`. No need to wrap it in `[]` – ctwheels May 03 '18 at 13:52
  • You may want to use `String#match` instead. – 31piy May 03 '18 at 13:56
  • I need the index of each match, I immagine I would need to loop regex – Jose Ospina May 03 '18 at 13:57
  • The duplicate answers your title, the answer to your question is this: the parenthesis are superfluous. You create a group by using them and match returns the matching string followed by all groups. That's why it is repeated twice. Remove them on you only get the matching string `/@[\w]+/g` – Prinzhorn May 03 '18 at 13:59

0 Answers0