Skip to main content
rule CX Codex

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

Install into Claude Code · Manually: put the files in place All methods and platforms ↓
.claude/rules/js-hoist-regexp.md
Open on GitHub

What 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 test returns a different answer on the same string

Example prompts for the agent

  • The regex in Highlighter is created inside render — hoist it to module scope or memoize it with useMemo().
  • Scan search.tsx for new RegExp in the component body and suggest a memoized version.
  • Find patterns with the g flag reused across calls and explain what lastIndex does to them.

How to install js-hoist-regexp

Manually: put the files in place

.claude/rules/js-hoist-regexp.md

The paths frontmatter field limits the files

Step 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

Similar IDE rules

All IDE rules for Frontend and UI →

From the same set130

The whole set →
rerender-defer-reads The rerender-defer-reads rule keeps callback-only reads of dynamic state out of component subscriptions. Use it when searchParams or localStorage is needed only after an action. CX Codex +1 ★ 7,191repo stars rendering-conditional-render The rendering-conditional-render rule prevents accidental rendering of zeros and NaN in JSX. It enforces explicit ternary operators instead of && checks, so falsy values stay out of the DOM. CX Codex +1 ★ 7,191repo stars server-serialization server-serialization is a Claude Code rule for limiting data passed across React Server/Client boundaries. It applies when client components receive props from server components. CX Codex +1 ★ 7,191repo stars rerender-simple-expression-in-memo The rerender-simple-expression-in-memo rule advises against using useMemo for simple expressions that return a boolean, number, or string. It applies when reviewing React component calculations. CX Codex +1 ★ 7,191repo stars rendering-hoist-jsx rendering-hoist-jsx extracts static JSX elements outside components to prevent element re-creation on every render pass. CX Codex +1 ★ 7,191repo stars rerender-transitions The rerender-transitions rule marks frequent, non-urgent state changes as transitions. It fits React code where updates such as scroll tracking should keep the interface responsive. CX Codex +1 ★ 7,191repo stars advanced-use-latest advanced-use-latest — a Claude rule file on reaching fresh callback values inside a React effect and keeping the dependency array down to state. CX Codex +1 ★ 7,191repo stars async-api-routes async-api-routes prevents sequential waterfall requests in API routes and Server Actions. It instructs the assistant to launch independent promises immediately rather than chaining awaits. CX Codex +1 ★ 7,191repo stars

Project sponsors

Companies that keep this analytics open