The RegExpRouter is Hono’s default and most performant router implementation. It uses a sophisticated trie-based algorithm to compile all routes into a single optimized regular expression, eliminating linear loops entirely.
The RegExpRouter builds separate matchers for each HTTP method, optimizing for the most common case where different methods have different route structures:
// Each method gets its own optimized matcherapp.get('/users/:id', getUser)app.post('/users', createUser)app.put('/users/:id', updateUser)app.delete('/users/:id', deleteUser)
Wildcard middleware is intelligently applied to routes during the build phase:
// Middleware is attached to routes at build timeapp.use('/api/*', authMiddleware)// These routes will include authMiddlewareapp.get('/api/users', listUsers)app.get('/api/posts', listPosts)
// ❌ Duplicate parameter namesapp.get('/:user/posts/:user', handler) // Will throw UnsupportedPathError// ❌ Wildcard with parameterapp.get('/files/:name/*', handler) // Will throw UnsupportedPathError// ❌ Ambiguous routesapp.get('/:a/b', handler1)app.get('/a/:b', handler2) // May throw during match
// Conceptual build flow1. Routes collected during app.get(), app.post(), etc.2. On first match() call: a. Routes sorted by static vs dynamic b. Static routes stored in hash map c. Dynamic routes inserted into trie d. Trie compiled to RegExp e. Capture group indices mapped to parameters3. Compiled matchers cached for subsequent requests
// Conceptual match flow1. Check static map: O(1) lookup2. If not found, execute compiled RegExp: O(1) match3. Extract capture groups using index map4. Return handlers with parameters