Regex, match "private function", "public function" or just "function"
I am getting some problems while trying to use this regex:
(public +function|private +function|function) +([a-zA-Z_$][0-9a-zA-Z_$]*) *\\(([0-9a-zA-Z_$, ]*)\\) *{(.*)}
For some reason, $1 and $2 are returning the same value.
The target string is:
public function messenger(text){
sendMsg(text);
}
private function sendMsg(text){
alert(text);
}
How can I fix that? By the way, I am using Javascript.
EDIT:
Ok, the answers开发者_JAVA技巧 worked, but the problem now is that the last paramenter is returning "sendMsg(text); } private function sendMsg(text){ alert(text);}", it is not stopping in the first "}"
actually, this was wrong, or at least misleading
see Regex is capturing the whole string for the improved version
| has a very low priority, you probably want
(public +function)|(private +function)|(function)
or even
(public|private +)?function
As a side note, regexp is not the best tool for parsing programming languages' grammars. For example, a regexp solution will fail on something like
var foo = "private function";
Would something like this be what you're looking for?
(public |private +)?function
This will check for either public, private, and the ?
signifies those are optional so only function
will work too. Just tested it using regexpal.com
Try
(public | private)?\sfunction
Beware tho, this could match: private private public private public function function
. If you can assure that the code is "clean" and doesn't have this kind of scenarios then use it.
精彩评论