2

I have a crazy string, something like:

sun #plants #!wood% ##arebaba#tey   travel#blessed    #weed das#$#F!@D!AAAA

I want to extract all "words" (also containing special characters) that begin with # or that have a space right before, taking the following as a result:

[
  'sun',
  'plants',
  '!wood%',
  'arebaba',
  'tey',
  'travel',
  'blessed',
  'weed',
  'das',
  '$',
  'F!@D!AAAA'
]

How do I get this using regex?

2 Answers 2

3

You can use match using regex: [^#\s]+:

var str = 'sun #plants #!wood% ##arebaba#tey   travel#blessed    #weed das#$#F!@D!AAAA';
    
var arr = str.match(/[^\s#]+/g);

console.log(arr);

RegEx Demo

Sign up to request clarification or add additional context in comments.

4 Comments

There's only one problem, it´s matching spaces if the string is just spaces, like " ". Should return an empty array [].
Can you clarify, how is that related to your question title of Extract hashtags from complex string using regex?
For example, if the user type only some spaces in the input and I match the hashtags he typed using using your regex, it will not return an empty array, since the user does not typed any hashtag. In this case should return [].
@IbnClaudius - If you require a hashtag or space to validate the input, just test it for [ #][^ #]|[^ #][ #] ahead of time, then get the array with [^ #]+
0

Just using match you could get all the group 1 matches into an array.

(?:^|[ #]+)([^ #]+)(?=[ #]|$)

Easy!

 (?: ^ | [ #]+ )
 ( [^ #]+ )                    # (1)
 (?= [ #] | $ )

Or, if you feel it's this simple, then just use ([^ #]+) or [^ #]+
which gets the same thing (like split in reverse).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.