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)="";