Regex to Match JavaScript Arrow Functions

Match JavaScript ES6 arrow function expressions with single parameter or parenthesized parameters. Captures the arrow syntax pattern. Free regex tester.

Regular Expression

/(?:(?:[a-zA-Z_$][a-zA-Z0-9_$]*)|\([^)]*\))\s*=>/g

Token Breakdown

TokenDescription
(?:Start of non-capturing group
(?:Start of non-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
|Alternation — matches the expression before OR after the pipe
\(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
)End of group
\sMatches any whitespace character (space, tab, newline)
*Matches the preceding element zero or more times (greedy)
=Matches the literal character '='
>Matches the literal character '>'

Detailed Explanation

This regex matches JavaScript arrow function expressions. Here is the token-by-token breakdown:

(?: — Opens a non-capturing group for the parameter alternatives.

(?:[a-zA-Z_$][a-zA-Z0-9_$]*) — First alternative: matches a single parameter without parentheses. The parameter name must start with a letter, underscore, or dollar sign, followed by zero or more alphanumeric characters, underscores, or dollar signs. This matches the concise syntax like x => x * 2.

| — Alternation operator separating the two parameter forms.

([^)]*) — Second alternative: matches parenthesized parameters. The escaped parentheses match literal opening and closing parens, with zero or more non-closing-paren characters inside. This handles zero parameters (), single parameters (x), and multiple parameters (a, b, c).

) — Closes the non-capturing group.

\s* — Matches optional whitespace between the parameters and the arrow.

=> — Matches the literal arrow operator consisting of an equals sign followed by a greater-than sign. This is the defining syntax element of arrow functions.

The g flag enables global matching to find all arrow functions in the source code. Arrow functions were introduced in ES6 and provide a concise syntax for function expressions. They also lexically bind the this value, making them particularly useful in callbacks and event handlers.

This pattern is useful for code analysis, identifying modern JavaScript patterns, and migration tools that convert traditional functions to arrow syntax. It matches both expression-body and block-body arrow functions since the body follows the => which this pattern captures.

Example Test Strings

InputExpected
x => x * 2Match
(a, b) => a + bMatch
() => console.log('hi')Match
function add(a, b) {}No Match
item => item.idMatch

Try It — Interactive Tester

//g
gimsuy

Match Highlighting(4 matches)

x => x * 2 (a, b) => a + b () => console.log('hi') function add(a, b) {} item => item.id

Matches & Capture Groups

#1x =>index 0
#2(a, b) =>index 11
#3() =>index 27
#4item =>index 73
Pattern: 47 charsFlags: gMatches: 4

Ctrl+Shift+C to copy regex

Customize this pattern →