Here is a little function which takes a string and returns an array of hashtags.

If you give:

'#HelloWorld, this is a #test'  

It will return:

['HelloWorld', 'test']

Here the code:

// hasgtag.js:

/* Extract hashtags text from string as an array */
function getHashTags(inputText) {  
    var regex = /(?:^|\s)(?:#)([a-zA-Z\d]+)/gm;
    var matches = [];
    var match;

    while ((match = regex.exec(inputText))) {

    return matches;

module.exports = getHashTags;  

And here are the mocha tests:

// test_hashtag.js:

var expect = require('chai').expect;

describe('Test hashtag extracting function', function() {  
    var getHashTags = require('./hashtag.js');

    it('should extract the hashtags', function(done) {
        var input = '#begin foobar #Has #Hell123 #hello and #what, #ending';
        var expectedResult =
            ['begin', 'Has', 'Hell123', 'hello', 'what', 'ending'];

        var result = getHashTags(input);


    it('should return an empty array', function(done) {
        var input = 'foo bar.';
        var result = getHashTags(input);


To run the tests:

$ npm install mocha
$ mocha test_hashtag.js


If you want to test your regular expressions, you can use this tools: