Extract hashtags from complex string using regex

后端 未结 2 796
無奈伤痛
無奈伤痛 2020-12-21 15:42

I have a crazy string, something like:

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

I want to extract all \"

相关标签:
2条回答
  • 2020-12-21 16:22

    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).

    0 讨论(0)
  • 2020-12-21 16:25

    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

    0 讨论(0)
提交回复
热议问题