Funky Kong
Funky Kong

Reputation: 77

Regex: Exclude numbers when matching hashtags

I have the following regex /#(\w+)/g which I am using to identify hashtags in a video description. This works however it also picks up numbered lists, i.e., #2. How can I exclude these while still detecting hashtags?

Here is a more detailed example of what I want to include and exclude: https://regex101.com/r/PGsAfh/5

Upvotes: 0

Views: 130

Answers (1)

Sweeper
Sweeper

Reputation: 270995

You can use this regex:

#\w*[a-zA-Z]\w*

It basically means that after the #, you can have any word character you like \w*, but there's gotta be a letter [a-zA-Z] somewhere. I have used * to allow the letter to appear at the start and end of the hashtag, and I have put \w* on both sides to allow numbers to be at the start and end of the hashtag.

Demo

Upvotes: 1

Related Questions