Skip to content

Instantly share code, notes, and snippets.

@oshikryu
Created April 30, 2013 20:43
Show Gist options
  • Save oshikryu/5491798 to your computer and use it in GitHub Desktop.
Save oshikryu/5491798 to your computer and use it in GitHub Desktop.
parse hash tags
// parse a string input or text body for #
//
function parseHashTags(words) {
var str_array = words.split(" ");
var pattern = /^\#/gi;
// the specific str_array element being processed
var part = "";
// rebuilt string of original title with hyperlinked words
var processed = "";
// loop through array of strings
for(var ii = 0; ii < str_array.length; ii++) {
// make it a hyperlink if word starts with #
// known error case e.g. #b#s#o#d
if(str_array[ii].match(pattern) && str_array[ii].length>1){
try{
// appends str_array element without the hash
part = '<a href="/search?trend='+str_array[ii].slice(1,str_array[ii].length)+'" class="hashcss">'+str_array[ii]+'</a>';
}
catch (e){
// error with hash
console.log('you sneaky fox');
}
}
// leave it as is
else {
part = str_array[ii];
}
// rebuilding the title
processed+=(part + " ");
}
return processed;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment