Skip to content

fix(tutorials): escape regex metacharacters in search highlighting - #3061

Open
zunixport wants to merge 1 commit into
MetaMask:mainfrom
zunixport:fix/tutorials-search-regex-crash
Open

zunixport wants to merge 1 commit into
MetaMask:mainfrom
zunixport:fix/tutorials-search-regex-crash

Conversation

@zunixport

@zunixport zunixport commented Sep 29, 2026 •

Copy link
Copy Markdown

Fix: crash in Tutorials search highlighting

Problem

highlightSearchText() in src/components/TutorialsPage/TutorialCard.tsx built a RegExp
straight from the user's search input, without escaping regex metacharacters:

const searchTerms = searchInput.trim().split(/\s+/)
const regex = new RegExp(`(${searchTerms.join('|')})`, 'gi')

Ordinary search text therefore produced invalid patterns — c++ → (c++) (SyntaxError: Nothing to repeat),
( → ((|) (unterminated group), [ → ([|) (unterminated character class).

Because new RegExp() is called during render, the throw is unhandled: the /tutorials
page crashes. The offending text stays in the search box, so the user cannot recover
without a full page reload.

The same input also highlighted matches incorrectly: . matched any character and
| acted as alternation instead of a literal.

Fix

Escape every search term before joining it into the pattern:

const searchTerms = searchInput
  .trim()
  .split(/\s+/)
  .map(term => term.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))
const regex = new RegExp(`(${searchTerms.join('|')})`, 'gi')

Search terms are now always matched literally, and the page no longer throws.

Scope

  • One file, one function: src/components/TutorialsPage/TutorialCard.tsx (the component actually
    rendered by TutorialsPage/index.tsx).
  • No behavior change for normal alphanumeric searches; filtering logic in index.tsx
    already uses String.includes() and was untouched.
  • Follow-up (not in this PR): GuideCard.tsx is not imported anywhere, and index.tsx keeps an
    unused copy of the old highlightSearchText. Both should be removed separately to keep the
    diff here minimal.

Note

Low Risk
Single-function UI change with no auth, data, or API impact; reduces render-time crash risk on the tutorials page.

Overview
Fixes /tutorials page crashes when the search box contains regex special characters (e.g. c++, (, [).

highlightSearchText in TutorialCard.tsx now escapes each search term before building the highlight RegExp, so terms are matched literally and new RegExp() no longer throws during render. Normal alphanumeric searches behave the same; only previously broken or incorrectly interpreted patterns are fixed.

Reviewed by Cursor Bugbot for commit 1c5e2fc. Bugbot is set up for automated code reviews on this repo. Configure here.

@vercel

vercel Bot commented Sep 29, 2026

Copy link
Copy Markdown

Someone is attempting to deploy a commit to the Consensys Team on Vercel.

A member of the Team first needs to authorize it.

@zunixport
zunixport requested review from a team as code owners September 29, 2026 18:15

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant