When mentioning users, if I mention one user more than once, I get string like:
"@[Firstname Lastname](4652) @[Firstname Lastname](4652) @[Firstname Lastname](4652)"
I tried to filter so the payload contains only one of those values. (@[Firstname Lastname](4652)
) The first problem I occured is the space is optional, so I couldn't split the array (I created an array out of string) by space. If I split it with @, it doesn't send the @ in the payload.
I assume I should create some kind of regex which will compare text between '@' and last ')', based on uses's ID which is in the brackets. But I was unable to create one.
Another issue is that users can write text as well, not just mention users, so it could look like:
"bla bla @[Firstname Lastname](4652) bla bla @[Firstname Lastname](4652) bla bla"
CodePudding user response:
You can first split the entire string using split(" ")
const string = "bla bla @[Firstname Lastname](4652) bla bla @[Firstname
Lastname](4652) bla bla";
const stringArr = string.split(" "); //Split the string into array of strings
let users = [];
const userPattern = /@[a-z]*)/i; //Begins with `@` and ends with `)`
stringArr.forEach((item) => {
if(userPattern.test(item)
{
users.append(item);
}
});
users = unique(users);
Function to remove duplicates
function unique(a) {
var seen = {};
var out = [];
var len = a.length;
var j = 0;
for(var i = 0; i < len; i ) {
var item = a[i];
if(seen[item] !== 1) {
seen[item] = 1;
out[j ] = item; //Adding unique arrays to this array
}
}
return out;
}
Hope this helps