javascript - Regex to wrap in an anchor matched hashtags in description -
data model:
var sample = {desc: "i #cool #cool1234 hello world", tags:["cool", "cool1234"]}
so able come function:
return sample.desc.replace(/#[a-z]+/g, "<a class=\"tags-in-details\" href=\"#/tab/myprofile-hashtags/$&\">$&</a>");
the problem function doesn't replace capitals "#cool" not linked , doesn't include numbers "#cool" linked without "1234".. in href desired url
href"#/tab/myprofile-hashtags/cool"
instead of
href="#/tab/myprofile-hashtags/#cool"
questions
what should regex upper case letters , numbers linked
how remove '#sign' in url (..url/cool instead of ..url/#cool)
screenshot app:
var sample = {desc: "i #cool #cool1234 hello world #notatag", tags:["cool", "cool1234"]}; var result = sample.desc.replace(/#(?:([a-za-z0-9]+))/g, function() { var tag = regexp.$1; if (sample.tags.indexof(tag) !== -1) { return "<a class=\"tags-in-details\" href=\"#/tab/myprofile-hashtags/" + tag + "\">" + tag + "</a>"; } return regexp.lastmatch; }); snippet.log(result);
<!-- provides `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
Comments
Post a Comment