javascriptcolorsnews-ticker

How to detect symbol ( # ) with javascript?


i am making live news ticker. like twitter, if any hastagh ( #blabla ), that's be color red..

Anyone can help me? Thanks in advance


Solution

  • You are probably looking for indexOf()

    The indexOf() method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found.

    Also as others have commented and if you are looking for a regex then this one will work for you as well

    (^|\s)(#[a-z\d][\w-]*) 
    

    DEMO