Java Stack Trace Frame Regex for JavaScript
/^\s+at ([a-zA-Z_$][a-zA-Z0-9_$]*(?:\.[a-zA-Z_$][a-zA-Z0-9_$]*)+)\.([a-zA-Z_$<>][a-zA-Z0-9_$<>]*)\(([a-zA-Z_$][a-zA-Z0-9_$]*\.java|Native Method|Unknown Source)(?::([0-9]+))?\)$/What this pattern does
This page provides a comprehensive, battle-tested regular expression for matching java stack trace frame, ported and verified for JavaScript. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. The snippet below is ready to drop into your JavaScript project — whether you're validating in an Express middleware, a Next.js API route, or a client-side form.
Javascript Implementation
// Java Stack Trace Frame
// ReDoS-safe | RegexVault — Dev & Systems > Log Parsing
const javaStackTraceFrameRegex = /^\s+at ([a-zA-Z_$][a-zA-Z0-9_$]*(?:\.[a-zA-Z_$][a-zA-Z0-9_$]*)+)\.([a-zA-Z_$<>][a-zA-Z0-9_$<>]*)\(([a-zA-Z_$][a-zA-Z0-9_$]*\.java|Native Method|Unknown Source)(?::([0-9]+))?\)$/;
function validateJavaStackTraceFrame(input: string): boolean {
return javaStackTraceFrameRegex.test(input);
}
// Example
console.log(validateJavaStackTraceFrame(" at com.example.MyClass.myMethod(MyClass.java:42)")); // trueTest Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
at com.example.MyClass.myMethod(MyClass.java:42) | at com.example.Class.method(File.java:10) |
at java.lang.Thread.run(Thread.java:833) | com.example.Class.method(File.java:10) |
at com.example.App.<init>(App.java:10) | at bad format |
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | — |
When to use this pattern
This pattern is drawn from the Dev & Systems > Log Parsing category and carries a ReDoS-safe certification. That matters for JavaScript developers because especially critical in long-running Node.js event loops where a ReDoS vulnerability can block the entire process. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.
Common Pitfalls
Lambda expressions and anonymous classes appear as ClassName$$Lambda$N or ClassName$1. The $ in class names must be in character classes.
Technical Notes
Groups: 1=FQCN, 2=method name, 3=source file or 'Native Method'/'Unknown Source', 4=line number. Leading whitespace is required. <init> and <clinit> are valid method names.
Have a pattern that belongs in the vault?
Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.
Submit a Pattern