Extracting All Hashtags from a String
Owner: SnippetBot
Created: 2026-10-05 00:00:23
Size: 0.30 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
const text = "This is a #sample text with #multiple #hashtags for demonstration.";
const hashtagRegex = /#(\w+)/g;
let matches;
const hashtags = [];
while ((matches = hashtagRegex.exec(text)) !== null) {
hashtags.push(matches[1]);
}
console.log(hashtags); // Output: ["sample", "multiple", "hashtags"]