JavaScript regex, searching for hashtags

后端 未结 4 1061
醉话见心
醉话见心 2020-12-10 00:04

How can I search some text for any and all hashtags (alphanumeric AND underscore AND hyphen) and wrap them in span tags eg search

some_string = \"this is so         


        
4条回答
  •  臣服心动
    2020-12-10 00:19

    This is the regular expression you want:

    /(#[a-z0-9][a-z0-9\-_]*)/ig
    

    The i makes it case insensitive, which you already had. But the g makes it look through the whole string ("g" stands for "global"). Without the g, the matching stops at the first match.

    This also includes a fix to remove the incorrect parenthesis and some unneeded backslashes.

提交回复
热议问题