split vs regex

Edit on GitHub

Setup

body = 'this is my first post but #yolo! #and #so you know how it works'

Tests

  • split

    var words = body.split(' ');
    var n = words.length;
    var hashtags = [];
    while (n--) {
        var w = words[n];
        if (w[0] === '#') hashtags.push(w);
    }
  • regex

    body.match(/(^|\s)(#[a-z\d-]+)/g);