Files
30-seconds-of-code/snippets/getBaseURL.md
Isabelle Viktoria Maciohsek 27c168ce55 Bake date into snippets
2021-06-13 13:55:00 +03:00

20 lines
504 B
Markdown

---
title: getBaseURL
tags: string,browser,regexp,beginner
firstSeen: 2020-05-03T12:20:54+03:00
lastUpdated: 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.
```js
const getBaseURL = url => url.replace(/[?#].*$/, '');
```
```js
getBaseURL('http://url.com/page?name=Adam&surname=Smith');
// 'http://url.com/page'
```