Regex for hashtag

/(?<![\w#])#[\p{L}\p{N}_]+/gu

Options
Matches

Examples

Note

The u flag lets \p{L} match letters in any script. The lookbehind skips a # inside a word, like C#.

Matches in the example

  • "#regex"
  • "#日本語"
  • "#100DaysOfCode"

Token by token

  1. (?<! not preceded by (negative lookbehind)
  2. [\w#] one of: a word character (letter, digit or _) or “#”
  3. ) end of group
  4. # the character “#”
  5. [\p{L}\p{N}_]+ one of: a character in the Unicode property “L”, a character in the Unicode property “N” or “_”, one or more times, as many as possible

Other patterns