开发者

JS regex match a word option with no backref

开发者 https://www.devze.com 2023-03-17 02:42 出处:网络
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|protecte

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].

0

精彩评论

暂无评论...
验证码 换一张
取 消