javascripthashtag

Extracting hashtags out of a string.


If I had a string as such

var comment =  "Mmmm #yummy #donut at #CZ"

How can I get a list of hash tags that exist in the string variable?

I tried using JavaScript split() method but I have to keep splitting all strings created from the initial split string. Is there a simpler way of doing this?


Solution

  • This will do it for anything with alphabetic characters, you can extend the regexp for other characters if you want:

    myString.match(/#[a-z]+/gi);