Tweak existing regex tag match
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I feel like I'm so close on this using this

/#([^\s]+)(?=[\s,#,;]|$)/gmi 

Link to what I've been fiddling with at regex101

I want to match some tags like #foo and #bar;

Here is a complete list of what to match:

#foo - currently works
#bar; - currently works
#baz# - currently works
#buz buz# - currently misses the second word because the 1st group excludes spaces. I want this to allow a space if there is a teminating # immediately after another word.
#"foo bar" - I want to allow internal spaces if double quoted
#"foo bar"# - optional since the ending # is extraneous
#'foo bar' - I want to allow internal spaces if single quoted
#'foo bar'# - optional since the ending # is extraneous

Do not match:

foo# bar - currently works since the 1st capturing group excludes a space after the #
foo#bar - I want to exclude matches where the # doesn't start the string or are preceeded by a non-space character
##foo - currently works - I don't want to match since # is duplicated
##foo bar##
##"foo bar"
##foo bar'

Also: I'll be implementing this in JavaScript, which I just learned doesn't have lookbacks.

I'll tip $25 for a winning answer today.

awarded to julianobs

Crowdsource coding tasks.

1 Solution


Matches all desired, excludes all undesired. Quick solution using OR.

((^| )#([^\s#"']+)(?=[\s,#,;]|$))|((^| )#["']([^#"']+)['"]#?(?=[\s,#,;]|$))|((^| )#([^#]+)[#;](?=[\s,#,;]|$))

Let me know if it's fine.

ps: link to solution on regex101 http://regex101.com/r/uS0aK5/1

Very nice - and fast! Thank you!
dhrubins 5 years ago
A pleasure!
julianobs 5 years ago