Regex for hashtag
/(?<![\w#])#[\p{L}\p{N}_]+/gu
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
(?<!not preceded by (negative lookbehind)[\w#]one of: a word character (letter, digit or _) or “#”)end of group#the character “#”[\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
- Email address
- URL
- IPv4 address
- UUID
- Hex color
- Date (YYYY-MM-DD)
- Time (24-hour)
- US phone number
- Phone number (E.164)
- US ZIP code
- Credit card number
- URL slug
- Username
- Strong password
- HTML tag
- Semantic version
- MAC address
- JSON Web Token
- Hashtag
- @mention
- Number
- Leading and trailing whitespace
- Repeated word
- Blank lines
- Markdown link
- Quoted string
- Domain name
- File extension
- Base64
- Trailing comma