-1

I am trying to create a method in Javascript which can return the username of the nth recipient in the message. The usernames can contain letters, numbers, underscores and hyphens so I need to do a regex check: /^[ A-Za-z0-9_-.\s]*$/i The method should receive two parameters - the message and the position of the recipient. If the recipient isn't found then the method should return an empty string.

This is my function:

getRecipient = function(message, position) {
    var message = "@User_One @UserABC! Have you seen this from @Userxyz?";
};

This is how it should work:

getRecipient(message,1)="User_One";
getRecipient(message, 2)="UserABC";
getRecipient(message, 3)="Userxyz";
getRecipient(message, 4)="";
Neelam Khan
  • 1,016
  • 8
  • 24
  • Instead of a regex _check_, you should do a regex _match_. Get all possible usernames into an array, then pick from that via index. – CBroe May 10 '22 at 10:21
  • Something like `console.log("@User_One @UserABC! Have you seen this from @Userxyz?".match(/@[A-Z0-9_-]+/gi))` – CBroe May 10 '22 at 10:25

0 Answers0