JavaScript regex, searching for hashtags -
how can search text , hashtags (alphanumeric , underscore , hyphen) , wrap them in span tags eg search
some_string = "this text 3 hashtags #tag1 , #tag-2 , #tag_3 in it"
and convert to:
"this text 3 hashtags <span>#tag1</span> , <span>#tag-2</span> , <span>#tag_3</span> in it"
i've got far:
some_string = some_string.replace(/\(#([a-z0-9\-\_]*)/i,"<span>$1</span>");
but 1 fault doesn't include # in wrappings should. seems output:
"this text 3 hashtags <span>tag1</span> , #tag-2 , #tag_3 in "
also detects first hashtag comes across (eg. #tag1
in sample), should detect all.
also need hashtags minimum of 1 character after #. # on own should not match.
thanks
try replace call:
edit: if want skip http://site.com/#tag
kind of strings use:
var repl = some_string.replace(/(^|\w)(#[a-z\d][\w-]*)/ig, '$1<span>$2</span>');
Comments
Post a Comment