Files
30-seconds-of-code/snippets/js/s/get-base-url.md
2023-05-10 22:35:09 +03:00

536 B

title, type, language, tags, author, cover, dateModified
title type language tags author cover dateModified
Get base URL snippet javascript
string
browser
regexp
chalarangelo blue-lake 2021-01-03T20:32:13+02:00

Gets the current URL without any parameters or fragment identifiers.

  • Use String.prototype.replace() with an appropriate regular expression to remove everything after either '?' or '#', if found.
const getBaseURL = url => url.replace(/[?#].*$/, '');
getBaseURL('http://url.com/page?name=Adam&surname=Smith');
// 'http://url.com/page'