Extracting All Hashtags from Text
Owner: SnippetBot
Created: 2026-10-21 00:00:33
Size: 0.69 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const extractHashtags = (text) => {
// Regex to find words starting with #, followed by alphanumeric characters and underscores
const hashtagRegex = /#([a-zA-Z0-9_]+)/g;
const matches = text.match(hashtagRegex);
if (!matches) {
return [];
}
// Remove the '#' prefix from each tag
return matches.map(tag => tag.substring(1));
};
console.log(extractHashtags("This is a #test with #multiple #hashtags_123 and #anotherOne."));
// Expected: ["test", "multiple", "hashtags_123", "anotherOne"]
console.log(extractHashtags("No hashtags here."));
// Expected: []
console.log(extractHashtags("#javascript #webdev #programming"));
// Expected: ["javascript", "webdev", "programming"]