Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,204 members, 7,818,694 topics. Date: Sunday, 05 May 2024 at 09:54 PM

Hashtag Highlighting Like Twitter With Javascript - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Hashtag Highlighting Like Twitter With Javascript (2201 Views)

Building A Word Scramble Game With Javascript (text Based) / How To Get A User IP Address With Javascript / I Bet You Cant Do This With Javascript, *check Out* (2) (3) (4)

(1) (Reply) (Go Down)

Hashtag Highlighting Like Twitter With Javascript by Cybergenius(m): 11:25pm On Aug 27, 2014
Hey folks i need your help. I would
like to know how to select the hashtags from a text with JavaScript (ex.
#hash) and then apply a html link to the hashtag as follows...
"DEMO":
some text...<a class="hashtag"
href="http://example.com?h=hash">#hash</a>
.
I tried something like this ->
"DEMO":

<script>
var text = "this is some text #foo with some hashtags";
text.replace(/#\w*[a-zA-Z]+\w*/ig,"hashtag name with html link"wink;
document.write(text);
</script>


this selects the hashtags, but how do i make the hashtag an html link. Thank you
Re: Hashtag Highlighting Like Twitter With Javascript by Nobody: 10:06pm On Aug 28, 2014
It's been a while I checked this section, na heavy school workload ooo!
Bros, manage this little code and let's see how it can be made even better with the contribution from others

linkify_hashtag=function(t){
t=t.replace(/(^|)#(\w+)/gi,function (s){return '<a rel="nofollow" href="?h='+s.replace(/#/,'%23') +'">'+s+'</a>';});
return t.replace(/\/@/gi,"/"wink;}

$(".hashtag"wink.each(function(){$(this).html(linkify_hashtag($(this).html()));});

I'm hereby calling all my ogas at d top ['dhtml18', 'webdezzi', 'asalimpo', 'codehouse', 'seun', ',,,'] and all javascript hackers in the house to come and make this code better ooo, let us learn something new from the well of your knowledge.
(probably shorter SLOC and faster speed)
Re: Hashtag Highlighting Like Twitter With Javascript by Nobody: 10:08pm On Aug 28, 2014
chai! so oga Seun neva give us syntax highlighter 4 dis department, diaris god oooo!
Re: Hashtag Highlighting Like Twitter With Javascript by Nobody: 10:49pm On Aug 28, 2014
^^^Hehehe
Re: Hashtag Highlighting Like Twitter With Javascript by Nobody: 2:28am On Aug 29, 2014
olucurious:
linkify_hashtag=function(t){
t=t.replace(/(^|)#(\w+)/gi,function (s){return '<a rel="nofollow" href="?h='+s.replace(/#/,'%23') +'">'+s+'</a>';});
return t.replace(/\/@/gi,"/" );}
$(".hashtag" ).each(function(){$(this).html(linkify_hashtag($(this).html()));});



<script type="text/javascript">

var linkify_hashtag=function(t){
t=t.replace(/(^|\s)#(\w+)/gi, "$1<a rel='nofollow' href='?h=%23$2'>#$2</a>" );
return t.replace(/\/@/gi,"/" );
}

document.write(linkify_hashtag("#sledjama" ));
document.write("<br>" );
document.write(linkify_hashtag("yes#sledjama" ));
document.write("<br>" );
document.write(linkify_hashtag("yes #sledjama" ));
</script>


of the 3 test scenarios, the second one should not be linkified, reason is we can have innocent URLs like http://webdezzi.com/index.php#tutorial
which we don't really want to touch
so why not set it that the # starts the line OR whitespace#

thats why i have the (^|\s) up there
and since you have gone the grouping route, why not ditch the function and just use the named groups directly for readability sake.
dont forget, the $1 is there to help return the whitespace back (if it exists)

if i know why you have the 3rd replace, i may be able to merge it into one, you are activating the regex engine 3x
even though your browser will gladly render it without visible drag, i think it is better to have just one

can you confirm if dot ( . ) is among twitter's allowed username characters because dot isn't regarded as a word in regex, atleast from my test

Nice work bro, the last line is one of the reasons i love jquery over traditional javascript
you just made a site-wide SELECTIVE search and replace look like bean
Re: Hashtag Highlighting Like Twitter With Javascript by Nobody: 2:45am On Aug 29, 2014
Oga mi sir, thank you sir. Waiting for the other ogas to come and disrupt this thread.
Re: Hashtag Highlighting Like Twitter With Javascript by Nobody: 2:51am On Aug 29, 2014
I am not going to say anything, too damn busy upgrading my site and webdezzi is here already.
Re: Hashtag Highlighting Like Twitter With Javascript by Nobody: 8:36am On Aug 29, 2014
dhtml18: I am not going to say anything, too damn busy upgrading my site and webdezzi is here already.

it is hard finding sleep these days, maybe the doctor can prescribe some tablets grin
Re: Hashtag Highlighting Like Twitter With Javascript by dhtml(m): 3:25pm On Aug 29, 2014
You are making too much money that is why.

(1) (Reply)

C Or C++, Which Is Best For Arduino Programming? / A Full-time Full-stack Developer Wanted In Ikeja / Finally Got A Badass Laptop After Months Of Saving, Come Celebrate With Me(pics)

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 24
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.