js-hoist-regexp — Claude Code rule: what it does, how to install
js-hoist-regexp — a Claude rule that keeps RegExp construction out of the render path: define the pattern at module scope or wrap it in useMemo().
MIT ★ 7,191 updated 2026-10-24
.claude/rules/js-hoist-regexp.mdWhat js-hoist-regexp does
The rule targets React components that build a pattern on every render. A regex created inline inside the component body gets compiled again on each pass; moving it above the component, or caching it with useMemo() keyed on the query string, stops that.
Two shapes sit side by side. One component calls new RegExp(...) in its body, the other keeps the same call inside useMemo with query as the dependency. A plain constant such as EMAIL_REGEX at module level covers patterns that never change between renders. Impact is labelled LOW-MEDIUM: small edit, noticeable on lists and highlighting.
Watch the flag. A pattern with g carries a mutable lastIndex, so a second test on the same input flips from true to false. Hoisting removes the recompiles; that shared state travels with the regex wherever you put it.
What it helps with
- stop a component from rebuilding its regex on every render
- move a static pattern out to module scope
- cache a query-driven pattern with
useMemo() - explain why
testreturns a different answer on the same string
Example prompts for the agent
- The regex in
Highlighteris created inside render — hoist it to module scope or memoize it withuseMemo(). - Scan
search.tsxfornew RegExpin the component body and suggest a memoized version. - Find patterns with the
gflag reused across calls and explain whatlastIndexdoes to them.
How to install js-hoist-regexp
Manually: put the files in place
.claude/rules/js-hoist-regexp.mdThe paths frontmatter field limits the files
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.cursor/rules/js-hoist-regexp.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.github/instructions/js-hoist-regexp.instructions.mdThe applyTo field sets which files it applies to
Manually: put the files in place
.devin/rules/js-hoist-regexp.mdManually: put the files in place
.clinerules/js-hoist-regexp.mdStep by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use
Questions about js-hoist-regexp
How do I install js-hoist-regexp in Claude Code?
Copy the rule file into your project at .claude/rules/js-hoist-regexp.md.
What does js-hoist-regexp do?
It tells the agent not to build RegExp objects during render: either the pattern lives at module level, or it is memoized with useMemo().
Why does a global regex return different results on the same string?
A /g pattern keeps lastIndex between calls, so test resumes where it stopped. The first call gives true, the next one on the same input gives false.
Source text
js-hoist-regexp.md · MIT
---
title: Hoist RegExp Creation
impact: LOW-MEDIUM
impactDescription: avoids recreation
tags: javascript, regexp, optimization, memoization
---
## Hoist RegExp Creation
Don't create RegExp inside render. Hoist to module scope or memoize with `useMemo()`.
**Incorrect (new RegExp every render):**
```tsx
function Highlighter({ text, query }: Props) {
const regex = new RegExp(`(${query})`, 'gi')
const parts = text.split(regex)
return <>{parts.map((part, i) => ...)}</>
}
```
**Correct (memoize or hoist):**
```tsx
const EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
function Highlighter({ text, query }: Props) {
const regex = useMemo(
() => new RegExp(`(${escapeRegex(query)})`, 'gi'),
[query]
)
const parts = text.split(regex)
return <>{parts.map((part, i) => ...)}</>
}
```
**Warning (global regex has mutable state):**
Global regex (`/g`) has mutable `lastIndex` state:
```typescript
const regex = /foo/g
regex.test('foo') // true, lastIndex = 3
regex.test('foo') // false, lastIndex = 0
```
© tech-leads-club · MIT · tech-leads-club/agent-skills