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 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”;