Search code examples
javascriptregexhashtag

Get hashtags from string with trim spaces and new lines


textarea text and result console log

The javascript code:

How can I find hashtags without newline or spaces or tabs just hashtags without url hashtags?

function findHashtags(searchText) {
    var regexp = /\#\w\w+\s?/g
    result = searchText.match(regexp);
    if (result) {
        result.map(function(s) { return s.trim() });
        console.log(result);
    } else {
        return false;
    }
}

Solution

  • Use \b instead of \s? - a word boundary instead of additional whitespace to not capture whitespace. Use \B (not a word boundary) to separate your url hashtags from urls that end in word characters.

    So:

    function findHashtags(searchText) {
        var regexp = /\B\#\w\w+\b/g
        result = searchText.match(regexp);
        if (result) {
            console.log(result);
        } else {
            return false;
        }
    }
    

    Which invoked thusly:

    findHashtags("http://www.masrawy.com/Sports/Sports_News/details/2014/9/5/338281#HPOFEATURE\n#ss\nddd\n#ddd jaaja ksks #sfsfsf\n#ggdg#hdhhd")
    

    Then returns:

    ["#ss", "#ddd", "#sfsfsf", "#ggdg", "#hdhhd"] 
    

    Note that this will fail if the url ends in anything but a word character (a-z0-9_). The only option besides that would be to capture and more effectively trim whitespace:

    function findHashtags(searchText) {
        var regexp = /(\s|^)\#\w\w+\b/gm
        result = searchText.match(regexp);
        if (result) {
            result = result.map(function(s){ return s.trim();});
            console.log(result);
            return result;
        } else {
            return false;
        }
    }