Detect a Hashtag

Discussion in 'jQuery' started by redslazers, Sep 27, 2011.

  1. #1
    Hi

    Thanks for anyone who can help me with this. I want jquery to do something if a particular hash tag is present (eg #hello) and then do something.

    if (present #hello)
    {do this}
    else
    {do this}

    Is this possible?
     
    redslazers, Sep 27, 2011 IP
  2. retwedia

    retwedia Member

    Messages:
    196
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    48
    #2
    Try this, let me know if it helps.

    
    hashtag_regexp = [COLOR=#800000]/#([a-zA-Z0-9]+)/[/COLOR]g;
    
    [COLOR=#00008B]function[/COLOR] linkHashtags(text) {
        [COLOR=#00008B]return[/COLOR] text.replace(
            hashtag_regexp,
            [COLOR=#800000]'<a class="hashtag" href="http://twitter.com/#search?q=$1">#$1</a>'[/COLOR]
        );
    } 
    
    $(document).ready([COLOR=#00008B]function[/COLOR](){
        $([COLOR=#800000]'p'[/COLOR]).each([COLOR=#00008B]function[/COLOR]() {
            $([COLOR=#00008B]this[/COLOR]).html(linkHashtags($([COLOR=#00008B]this[/COLOR]).html()));
        });
    });
    Code (markup):

    With above code the text containing the hash tag must be within <p></p>
     
    retwedia, Nov 8, 2011 IP