Urgent.News

One page, thousands of outlets. See who else covered it.

Editions

Tech

Stop Writing Regex to Match URLs — The Browser Already Can

Priya was three paragraphs into rewriting a support ticket when the page flashed and her draft reverted to what it had looked like an hour earlier. She hadn't refreshed. Nobody had. The service worker had. It was running a cache-first strategy for ticket pages — fetch once, serve from cache after that, so the dashboard felt instant on a flaky connection. The intent was to cache /tickets/482 , the…

In a support ticket, Priya encountered a recurring issue with a regex-based approach to detect specific pages in a web application. The regex pattern, const isTicketView = /^ \/ tickets \/\d +$/ , was designed to match URLs containing "/tickets/" followed by one or more digits. However, it failed to account for trailing slashes or query strings, resulting in incorrect matches and unexpected behavior.

To fix this problem, the solution lies in using a built-in browser API called URLPattern. URLPattern is a global constructor that was introduced in 2021 and provides a more reliable and straightforward way to match and parse URLs. This API eliminates the need for writing complex regular expressions and handles various edge cases automatically.

By utilizing URLPattern, developers can define specific URL patterns with named groups, which allows for both matching and extracting relevant information from the URL. For example, const ticketView = new URLPattern ({ pathname : /tickets/:id }); and const ticketEdit = new URLPattern ({ pathname : /tickets/:id/edit }); enable precise matching of different URL patterns without the risk of missing boundary conditions or introducing new bugs.

Using URLPattern offers several advantages over manual regex matching. It reduces the complexity of the code, improves readability, and provides a more consistent approach to URL handling. The API also allows for additional features such as wildcard matching, optional segments, and custom regex within named groups. These additional capabilities address various URL patterns encountered in real-world applications without the need for intricate regex patterns.

To utilize URLPattern, developers can create separate patterns for specific URL paths, such as ticket view and ticket edit pages. By executing these patterns against a given URL, developers can determine whether the URL matches the intended pattern. If a match is found, additional information, such as the captured groups, can be accessed using the exec() method.

In conclusion, the issue Priya faced with her regex-based URL matching can be easily resolved by leveraging the URLPattern API provided by modern web browsers. This built-in solution offers a more reliable, concise, and maintainable approach to URL matching compared to manually written regex patterns. By adopting URLPattern, developers can avoid the pitfalls of hand-rolled regex and ensure consistent and accurate URL matching throughout their applications.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Dashforge: an application orchestrator for React

React solved rendering. Dashforge tries to solve orchestration — theming, forms, permissions, and visibility moved out of your components, declaratively, predictably, reusably.

  • Dashforge is an application orchestrator for React
  • Uses react-hook-form as core with theming and forms
  • Improves performance through declarative reactions

1a vez trabalhando com git com time: tudo que você precisa saber

Faz mais de 5 anos que eu não abria um PR ou issue técnica no Github, mas essa semana tenho aprendido algumas boas práticas e termos que reuni neste artigo.

  • Git branches allow parallel work on projects without affecting the official version
  • Create a branch per contribution to isolate errors if something goes wrong
  • PR descriptions should be concise summaries, not line-by-line lists

Regex Against a PDF: The One Endpoint That Skips OCR Entirely

Most document pipelines have a reflex. A PDF comes in, and the first instinct is: run OCR, then parse it. That reflex costs time and money on documents that never needed it in the first place.

  • PDF4me endpoint extracts text from PDFs without OCR
  • Uses regular expression to pull desired text directly from text layer
  • Supports asynchronous processing with 202 Accepted status code

More from Thursday 20 August →