What and How to Hashtag from Strings in JavaScript - Kevin Uriel Fonseca
What’s a Hashtag?
Well, that is something more than just simply words followed by a #
character. These have the purpose of helping us categorize our data into a more user-friendly concept; an example of this can be found in several social media sites such as Twitter, Facebook, YouTube, etc.
Hashtags help make content discoverable in cross-platforms and thus more reachable to people. A hashtag makes the searching process easier, when a user sees a post that is of interest, they will likely spend time looking through content brought by the hashtag. A unique hashtag will also make your post stand out from the rest.
It is not only about reaching an audience.
Hashtags can also be used to identify products and services. They can be generalized or be very specific also. Also they can be a good starting-ground for you to explore, let’s say, if certain hashtag is simply not working, you can easily change it, just make sure to be patient, consistency is key.
Its use its simple, they don’t require too much wording but you also don’t want to create pollution. A pollution, in our case hashtag pollution would look something like this #hola #soyKevin #y #megusta #mucho #JavaScript . Always make sure to add only content-related hashtags and try to avoid adding too many unless you already have a good amount of readers.
As a solo Snippet
req.body.tags = [...req.body.text.matchAll(/#[\w\d\-_!\?]+/g)].map((match) => match[0].substr(1))
As a Function
exports.generateHashtags = (text) => { var toReturn = [], i = 0, hashtag = false let isNumber = (n) => Number(n).toString() == n //!isNaN(n) doesn't work properly let isValidChar = (c) => c == '_' ? true : isNumber(c) || c.toUpperCase() != c.toLowerCase() for (let c of text) { if (typeof toReturn[i] != 'string') { toReturn[i] = '' } //because I can't add a character to undefined if (c == '#') { hashtag = true continue } //hashtag found if (isValidChar(c) && hashtag) { toReturn[i] += c } //character of a hashtag word else if (hashtag) { hashtag = false i++ } //no longer the hashtag } return toReturn.filter((tag) => tag.length && !isNumber(tag)) //no empty values and a tag can't be ONLY a number}
Reference:
- Generate hashtags from string in JavaScript
- Why Are #Hashtags So Darn Important
- What is a Hashtag? And What Do You Do With Hashtags?
- How to Use Hashtags in 2021: A Quick and Simple Guide for Every Network
Bye-Bye 🙂