Skip to content

A regular expression to capture a URL without query string parameters

The Valley of Code

Your Web Development Manual

I had the need to capture a URL without query parameters.

For both http and https links.

Hereโ€™s the regex I used:


Hereโ€™s an example:

const regex = /^(http[^?#]+).*/gm
const result = regex.exec("")

[ '', 
index: 0, 
input: '', 
groups: undefined ]

โ†’ Get my JavaScript Beginner's Handbook
โ†’ Read my JavaScript Tutorials on The Valley of Code
โ†’ Read my TypeScript Tutorial on The Valley of Code
  • THE VALLEY OF CODE (+ PRO), your web development manual
  • I wrote 15+ free coding BOOKS, download them here
  • SOLOPRENEUR LAND the missing MBA for wannabe solopreneurs craving a life with more freedom, control, fulfillment and purpose (summer 2024)