Regex to Match JavaScript Function Declarations
Match JavaScript function declarations capturing the function name and parameters. Handles standard named function syntax. Free online regex tester.
Regular Expression
/function\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*\([^)]*\)/g
Token Breakdown
| Token | Description |
|---|---|
| f | Matches the literal character 'f' |
| u | Matches the literal character 'u' |
| n | Matches the literal character 'n' |
| c | Matches the literal character 'c' |
| t | Matches the literal character 't' |
| i | Matches the literal character 'i' |
| o | Matches the literal character 'o' |
| n | Matches the literal character 'n' |
| \s | Matches any whitespace character (space, tab, newline) |
| + | Matches the preceding element one or more times (greedy) |
| ( | Start of capturing group |
| [a-zA-Z_$] | Character class — matches any one of: a-zA-Z_$ |
| [a-zA-Z0-9_$] | Character class — matches any one of: a-zA-Z0-9_$ |
| * | Matches the preceding element zero or more times (greedy) |
| ) | End of group |
| \s | Matches any whitespace character (space, tab, newline) |
| * | Matches the preceding element zero or more times (greedy) |
| \( | Matches a literal opening parenthesis |
| [^)] | Negated character class — matches any character NOT in ) |
| * | Matches the preceding element zero or more times (greedy) |
| \) | Matches a literal closing parenthesis |
Detailed Explanation
This regex matches JavaScript function declarations in the standard syntax. Here is the token-by-token breakdown:
function — Matches the literal keyword function that begins a function declaration.
\s+ — Matches one or more whitespace characters between the keyword and the function name.
([a-zA-Z_$][a-zA-Z0-9_$]*) — Capturing group 1 matches the function name. JavaScript identifiers must start with a letter, underscore, or dollar sign, followed by zero or more letters, digits, underscores, or dollar signs. This captures names like myFunction, _private, $helper, and calculateTotal.
\s* — Matches optional whitespace between the function name and the opening parenthesis.
( — Matches the literal opening parenthesis that begins the parameter list. The parenthesis is escaped because it is a regex metacharacter used for grouping.
[^)]* — Matches zero or more characters that are not a closing parenthesis. This captures the entire parameter list including parameter names, default values, destructuring patterns, and commas between parameters.
) — Matches the literal closing parenthesis that ends the parameter list.
The g flag enables global matching to find all function declarations in the source code. This pattern is useful for code analysis, function indexing, documentation generation, and refactoring tools. It matches standard function declarations but not arrow functions, function expressions assigned to variables, or class methods. For those patterns, separate regex patterns are needed.
Example Test Strings
| Input | Expected |
|---|---|
| function hello() {} | Match |
| function add(a, b) { return a + b; } | Match |
| const fn = () => {} | No Match |
| function _private(x) {} | Match |
| class MyClass {} | No Match |
Try It — Interactive Tester
Match Highlighting(3 matches)
Matches & Capture Groups
49 charsFlags: gMatches: 3Ctrl+Shift+C to copy regex
Related Regex Patterns
Regex to Match JavaScript Arrow Functions
/(?:(?:[a-zA-Z_$][a-zA-Z0-9_$]*)|\([^)]*\))\s*=>/g
Regex to Match Programming Variable Names
/^[a-zA-Z_$][a-zA-Z0-9_$]*$/
Regex to Match JavaScript Import Statements
/import\s+(?:(?:\{[^}]*\}|\*\s+as\s+\w+|\w+)\s*(?:,\s*(?:\{[^}]*\}|\*\s+as\s+\w+|\w+)\s*)*from\s+)?['"][^'"]+['"]/gm
Regex to Match Single-Line Comments
/\/\/.*$/gm