Created
April 30, 2013 20:43
-
-
Save oshikryu/5491798 to your computer and use it in GitHub Desktop.
parse hash tags
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
// 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