Last active
May 31, 2018 20:45
-
-
Save darkrishabh/020c2a79e6ac4d46d44c to your computer and use it in GitHub Desktop.
JAVASCRIPT: Find all the hastags in a string (eg INPUT="Hi #my#name is #Bond #JamesBond" OUTPUT = ["my", "name", "Bond", "JamesBond"])
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _ = require('lodash'); | |
function HashTags(text) { | |
var adding = false; | |
var d = ""; | |
var t = ""; | |
return _.filter(_.map(text, function (c, k) { | |
if (c === '#' || (c === ' ')) { | |
if (adding) { | |
t = d; | |
d = ""; | |
if (c === ' ') { | |
adding = false; | |
} | |
return t; | |
} else { | |
adding = true; | |
} | |
} | |
if (adding && c !== '#') { | |
d += c; | |
} | |
if (k === text.length - 1) { | |
t = d; | |
d = ""; | |
return t | |
} | |
}), function (tag) { | |
tag = tag ? tag.trim() : tag | |
return tag | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment