Depending on how you define the word, you probably want to
(?<=\s|^)[@
or
(?<=\s|^)[@
Explanation:
(?<=\s|^)
So in PHP:
preg_match_all('/(?<=\s|^)[@#]\S+/', $subject, $result, PREG_PATTERN_ORDER);
gives an $result array of all matches in the string $subject . In JavaScript, this will not work because lookbehinds (the "Assert ..." part from the beginning of the regular expression) are not supported.
source share