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
| Token | Description |
|---|---|
| (?: | 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 |
| \s | Matches 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
| Input | Expected |
|---|---|
| x => x * 2 | Match |
| (a, b) => a + b | Match |
| () => console.log('hi') | Match |
| function add(a, b) {} | No Match |
| item => item.id | Match |
Try It — Interactive Tester
Match Highlighting(4 matches)
Matches & Capture Groups
47 charsFlags: gMatches: 4Ctrl+Shift+C to copy regex
Related Regex Patterns
Regex to Match JavaScript Function Declarations
/function\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*\([^)]*\)/g
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 Programming Variable Names
/^[a-zA-Z_$][a-zA-Z0-9_$]*$/
Regex to Match Single-Line Comments
/\/\/.*$/gm