2013-05-21 54 views
0

在这里,我试图弄清正则表达式的东西。 我创造了这个正则表达式:正则表达式匹配条件,但不返回它

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

它在鸣叫的所有用户和hastags匹配。 问题是他们返回条件(@ |#)和(\ s | $ | \ :)

是否有可能不返回它们?

我使用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 "] 
+0

你试过吗? http://stackoverflow.com/questions/432493/how-do-you-access-the-matched-groups-in-a-javascript-regex –

+0

谢谢。正则表达式的问题是我不知道要搜索什么或者我对功能的解释是什么。这使得很难找到所有的问题。 – HerrWalter

回答

4

如何:

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

解释:

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 
---------------------------------------------------------------------- 
+0

该死的。手指缓慢。 – FrankieTheKneeMan

+0

您是从哪里生成该描述的?或者你自己写了吗? – zzzzBov

+0

@zzzzBov:我用perl模块'YAPE :: Regex :: Explain'生成了它。 – Toto

1

这应该做的伎俩:/[@#]([^\s$:]+)/g

0

你有什么(即一个组不是类)

var match, re = /(@|#)(.*?)(\s|$|\:)/g; 
while (match = re.exec(a)) { 
alert(match[2]); // match[1] is "#" or "@" 
} 
相关问题