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

TokenDescription
fMatches the literal character 'f'
uMatches the literal character 'u'
nMatches the literal character 'n'
cMatches the literal character 'c'
tMatches the literal character 't'
iMatches the literal character 'i'
oMatches the literal character 'o'
nMatches the literal character 'n'
\sMatches 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
\sMatches 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

InputExpected
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

//g
gimsuy

Match Highlighting(3 matches)

function hello() {} function add(a, b) { return a + b; } const fn = () => {} function _private(x) {} class MyClass {}

Matches & Capture Groups

#1function hello()index 0
Group 1:hello
#2function add(a, b)index 20
Group 1:add
#3function _private(x)index 77
Group 1:_private
Pattern: 49 charsFlags: gMatches: 3

Ctrl+Shift+C to copy regex

Customize this pattern →