js-hoist-regexp

Hoist RegExp Creation

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "js-hoist-regexp" with this command: npx skills add theorcdev/8bitcn-ui/theorcdev-8bitcn-ui-js-hoist-regexp

Hoist RegExp Creation

Don't create RegExp inside render. Hoist to module scope or memoize with useMemo() .

Incorrect (new RegExp every render):

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):

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:

const regex = /foo/g regex.test('foo') // true, lastIndex = 3 regex.test('foo') // false, lastIndex = 0

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Coding

fumadocs-mdx-structure

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

rendering-animate-svg

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

8-bit-pixel-art-patterns

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

fumadocs-component-docs

No summary provided by upstream source.

Repository SourceNeeds Review