FlareStart
HomeNewsHow ToSources
FlareStart

Where developers start their day. All the tech news & tutorials that matter, in one place.

Quick Links

  • Home
  • News
  • Tutorials
  • Sources
  • Privacy Policy

Connect

© 2026 FlareStart. All rights reserved.

Back to articles
When Regex Meets the DOM (And Suddenly It’s Not Simple Anymore)
How-ToWeb Development

When Regex Meets the DOM (And Suddenly It’s Not Simple Anymore)

via Dev.to JavaScriptKristieene Knowles1mo ago

I recently built a custom in-page “Ctrl + F”-style search and highlight feature. The goal sounded simple: Support multi-word queries Prefer full phrase matches Fall back to individual token matches Highlight results in the DOM Skip <code> and <pre> blocks In my head? “Easy. Just build a regex.” Step 1: Build the Regex If a user searches: power shell I generate a pattern like: power [ \ s \ u00A0 ] + shell | power | shell The logic: Try to match the full phrase first If that fails, match individual tokens On paper? Clean. In isolation? Works. Step 2: Enter the DOM This is where things escalated. Instead of just running string.match() , I had to: Walk the DOM Avoid header UI Avoid <pre> , <code> , <script> , <style> Avoid breaking syntax highlighting Replace only text nodes Preserve structure That meant using a TreeWalker . const walker = document . createTreeWalker ( root , NodeFilter . SHOW_TEXT , { acceptNode ( node ) { const p = node . parentElement ; if ( ! p ) return NodeFilter . F

Continue reading on Dev.to JavaScript

Opens in a new tab

Read Full Article
19 views

Related Articles

Belkin’s battery-equipped Switch 2 case is more than 35 percent off right now
How-To

Belkin’s battery-equipped Switch 2 case is more than 35 percent off right now

The Verge • 14h ago

Why this Marshall is the first soundbar I've tested that truly challenges my Sonos Arc Ultra
How-To

Why this Marshall is the first soundbar I've tested that truly challenges my Sonos Arc Ultra

ZDNet • 15h ago

This App Makes Even the Sketchiest PDF or Word Doc Safe to Open
How-To

This App Makes Even the Sketchiest PDF or Word Doc Safe to Open

Wired • 15h ago

References: The Alias You Didn’t Know You Needed
How-To

References: The Alias You Didn’t Know You Needed

Medium Programming • 17h ago

Pointers: The Concept Everyone Says Is Hard
How-To

Pointers: The Concept Everyone Says Is Hard

Medium Programming • 17h ago

Discover More Articles