Back to skills
SKILL.md
Js Hoist Regexp
ASecurityImported skill js_hoist_regexp from vercel
- 3 stars
- 0 votes
- 0 copies
- 2 views
- Added September 11, 2026
Security analysis
100/100npx -y skills add bitwikiorg/skills.md --skill js_hoist_regexp --agent claude-codeAre you the author of Js Hoist Regexp?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/bitwikiorg-js-hoist-regexp)---
description: Imported skill js_hoist_regexp from vercel
name: js_hoist_regexp
signature: f9e9aef2f7c2307dd7310f283df85dcada45fc43e9bb941b0c3aa414dec21ec4
source: /a0/tmp/skills_research/vercel/skills/react-best-practices/rules/js-hoist-regexp.md
---
---
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
```
Attribution
Comments
Loading comments…