I have a string from an input named 'text' and from this one, I would like to generate hashtags for a different field in my mongoose model:
req.body.tags = req.body.text
.split('#')
.map((tag) =>
tag.trim().replace(/ +/g, ' ').split(' ').join('-').toLowerCase()
)
.filter((tag) => tag.length !== 0)
The code above is almost perfect but every time, I press a comma it gets inserted as a hashtag(or part of it) which is something I'm trying to avoid as well, take a look into what I'm talking about:
{
"text": "Hola, mi nombre es Kevin y tu como te llamas? #random, #userundefined"
}
The text above is the data I insert via Postman and this is the output:
"tags": [
"hola,-mi-nombre-es-kevin-y-tu-como-te-llamas?",
"random,",
"userundefined"
],
What I would like to get is this:
"tags": [
"random",
"userundefined"
],
I just want to retrieve the words followed by a # and just that, I don't want the commas after it as shown in the random tag
?be apart of the the text allowed in tags?.text.match(/(#\w+)/g)?#rand?om