admin管理员组文章数量:1434943
I was wanting to organize all my javascript functions to put them in alphabetical order a month ago or so and was playing with a regular expression today that made me think of that again. I only want to match the outer functions to avoid nested functions getting moved and almost had it I think, but there is something I am missing.
I used bobble bubble's answer on this page as a starting point. Regular Expression to match outer brackets
function\s.*\(.*\).*\{(?>[^.*\n*]+|(?R))*\}
This will match all function definitions and capture the arguments up to the first curly brace. For some reason I can't get it to match the newlines pattern [^.*\n*]+
while it is part of the expression, but when it is all by itself it matches just fine.
I was using Sublime text editor as my tool to search, but it would end up in a JS file probably as an easy way to manipulate the code.
I was wanting to organize all my javascript functions to put them in alphabetical order a month ago or so and was playing with a regular expression today that made me think of that again. I only want to match the outer functions to avoid nested functions getting moved and almost had it I think, but there is something I am missing.
I used bobble bubble's answer on this page as a starting point. Regular Expression to match outer brackets
function\s.*\(.*\).*\{(?>[^.*\n*]+|(?R))*\}
This will match all function definitions and capture the arguments up to the first curly brace. For some reason I can't get it to match the newlines pattern [^.*\n*]+
while it is part of the expression, but when it is all by itself it matches just fine.
I was using Sublime text editor as my tool to search, but it would end up in a JS file probably as an easy way to manipulate the code.
Share Improve this question edited May 23, 2017 at 12:34 CommunityBot 11 silver badge asked Mar 23, 2017 at 21:49 AlanAlan 2,0962 gold badges23 silver badges48 bronze badges 2- 3 "to put them in alphabetical order" --- that's a terrible idea, really. The order must be semantic-driven. Anyway, if you want to solve it properly - take any JS syntax parser and it will be 100 times easier and more reliable. – zerkms Commented Mar 23, 2017 at 21:55
-
You will have to look for new lines.
\nfunction.*?\n\}
this may be it... – Akxe Commented Mar 23, 2017 at 21:59
2 Answers
Reset to default 4After a day of fiddling with it, here is a regex that will break up a js file to match all named functions and then break it up into function name, arguments, and body. Unlike Floribon's solution, this will match any formatting style, even minified, and ignores nested braces and functions.
function\s+(?<functionName>\w+)\s*\((?<functionArguments>(?:[^()]+)*)?\s*\)\s*(?<functionBody>{(?:[^{}]+|(?-1))*+})
https://regex101./r/sXrHLI/1
This seem to match all functions and their body for me (except the ones defined in one-line, which would require an additional expression)
function.*\(.*\).*\{(.|\n)*?\n\}
Or if you don't want to catch the body just add ?:
function.*\(.*\).*\{(?:.|\n)*?\n\}
The idea is to match until we finish by a new line and a closing curly bracket: this way no bracket (or inner functions) within the body will collide with our search.
本文标签: regexRegular expression to match all javascript functionsStack Overflow
版权声明:本文标题:regex - Regular expression to match all javascript functions - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745624679a2666900.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论