2012-06-23 60 views

回答

4

取決於你如何定義一個 「字」,你可能想

(?<=\s|^)[@#]\S+ 

(?<=\s|^)[@#]\w+ 

說明:

(?<=\s|^) # Assert that the previous character is a space (or start of string) 
[@#]  # Match @ or # 
\S+  # Match one or more non-space characters 
(or \w+) # Match one or more alphanumeric characters. 

因此,在PHP中:

preg_match_all('/(?<=\s|^)[@#]\S+/', $subject, $result, PREG_PATTERN_ORDER); 

爲您提供了字符串$subject中所有匹配的數組$result。在JavaScript中,這是行不通的,因爲lookbehinds(正則表達式開始處的「Assert ...」部分)不受支持。