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?
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);
Just use a regular expression to find occurences of a hash followed by non-whitespace characters.
"Mmmm #yummy #donut at #CZ".match(/#\w+/g)
// evaluates to ["#yummy", "#donut", "#CZ"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With