I am using the below function to replace words beginig with # from the string, with variables of exact same name from the extra_data object.
var messageString = "The folder #folder_name was removed from the workspace #workspace_name by #user_name"
var re = /(?:^|\W)#(\w+)(?!\w)/g, match;
while (match = re.exec(messageString)) {
messageString = messageString.replace(match[0],extra_data[match[1]]);
console.log("I am here--------------------------------------------->1");
console.log(messageString);
}
Console log
I am here--------------------------------------------->1
The folder23545 was removed from the workspace #workspace_name by #user_name
I am here--------------------------------------------->1
The folder23545 was removed from the workspace127 by #user_name
The above code replaces only 2 instances and sometimes eats up white space also as you can see at workspace127. What am i doing wrong?