If you're looking to include characters from any alphabet in hashtags, consider using this method:
let text = "улетные #выходные // #holiday in the countryside";
const hashtags = []
if (text.length) {
let preHashtags = text.split('#')
let i = 0;
if (text[0] !== '#') i++
for (null; i < preHashtags.length; i++) {
let item = preHashtags[i]
hashtags.push(item.split(' ')[0])
// String.prototype.split() is necessary to filter out non-hashtag related strings
}
}
console.log(hashtags) // outputs [ 'выходные', 'holiday' ]
We use if (text[0] !== '#') i++
to check if the first letter in the "text" string is not a '#'. If it's not, we skip iterating through the first element in the preHashtags Array. Otherwise, if our text string starts with a hashtag, we need to process it.
Remember to validate the resulting hashtags
array. The use of null
in the for loop is purely for readability purposes; you could also use
for (;i < preHashtags.length; i++)
This method ensures that all symbols, including those from non-Latin alphabets, are included, making it beginner-friendly and easy to understand. In terms of performance, it excels in Chrome (and similar browsers like node.js), but performs slightly less efficiently in Firefox and Safari, as shown in this test: .
Consider your platform - whether running code in node.js or a browser, especially if targeting MobileSafari users, when deciding on this approach.