0

Here I am trying to get something clear on regexes. I've created this regex:

a.match( /(@|#)(.*?)(\s|$|\:)/g )

It matches all users and hastags in a tweet. Problem is they return the condition( @|#) and (\s|$|\:)

Is it possible not returning them?

I'm using Javascript

var a ='RT @OLMJanssen: Met #FBKGames en @Jmvanhalst volop in voorbereiding: 6 juni seminar kwaliteitsborging van #sportaccommodatie bij regiseerende gemeente'
a.match( /(@|#)(.*?)(\s|$|\:)/g )
//returns ["@OLMJanssen:", "#FBKGames ", "@Jmvanhalst ", "#sportaccommodatie "]
HerrWalter
  • 622
  • 1
  • 5
  • 13
  • Have you tried this? http://stackoverflow.com/questions/432493/how-do-you-access-the-matched-groups-in-a-javascript-regex – Frison Alexander May 21 '13 at 14:54
  • Thank you. The problem with regex is that I dont know what to search for or what my interpretation of a functionality is called. Which makes it hard to find the allready awnsered questions. – HerrWalter May 21 '13 at 20:57

3 Answers3

4

How about:

a.match(/[@#](\S+)(?:\s|:|$)/g)

explanation:

The regular expression:

(?-imsx:[@#](\S+)(?:\s|:|$))

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  [@#]                     any character of: '@', '#'
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    \S+                      non-whitespace (all but \n, \r, \t, \f,
                             and " ") (1 or more times (matching the
                             most amount possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    \s                       whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    :                        ':'
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
----------------------------------------------------------------------
  )                        end of grouping
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
Toto
  • 89,455
  • 62
  • 89
  • 125
1

This should do the trick: /[@#]([^\s$:]+)/g

rtcherry
  • 4,840
  • 22
  • 27
0

With what you have (i.e a group not a class)

var match, re = /(@|#)(.*?)(\s|$|\:)/g;
while (match = re.exec(a)) {
 alert(match[2]); // match[1] is "#" or "@"
}
Alex K.
  • 171,639
  • 30
  • 264
  • 288