Let's say I have:
/(private|public|protected)\s+function\s+(\w+)\((.*)\)\s+{/gi
To match the beginning of a function decla开发者_如何学Goration. I don't want to use (private|public|protected)
because the (
and )
make for a backreference, but I can't use [
and ]
because they don't match the full words.
Basically, I only want the function name as $1
and arguments as $2
.
Thanks all.
** edit **
Per the answer, I used ?:
eg. /(?:private|public|protected)\s+function\s+(\w+)\((.*)\)\s+{/
You can use a non-capturing group:
(?: ... )
From the MDN documentation:
(?:x)
Matches'x'
but does not remember the match. These are called non-capturing parentheses. The matched substring can not be recalled from the resulting array's elements[1], ..., [n]
.
精彩评论