2013-05-21 83 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 "@" 
} 
相關問題