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:

/^(http[^?#]+).*/gm

Hereโ€™s an example:

const regex = /^(http[^?#]+).*/gm
const result = regex.exec("https://test.com?test=2")
console.log(result)

/*
[ 'https://test.com?test=2', 
'https://test.com', 
index: 0, 
input: 'https://test.com?test=2', 
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)