Update getBaseURL.md

This commit is contained in:
Angelos Chalaris
2021-01-03 20:32:13 +02:00
committed by GitHub
parent 057f8aca5d
commit d5269b2353

View File

@ -1,15 +1,14 @@
---
title: getBaseURL
tags: string,browser,beginner
tags: string,browser,regexp,beginner
---
Gets the current URL without any parameters.
Gets the current URL without any parameters or fragment identifiers.
- Use `String.prototype.indexOf()` to check if the given `url` has parameters, `String.prototype.slice()` to remove them if necessary.
- Use `String.prototype.replace()` with an appropriate regular expression to remove everything after either `'?'` or `'#'`, if found.
```js
const getBaseURL = url =>
url.indexOf('?') > 0 ? url.slice(0, url.indexOf('?')) : url;
const getBaseURL = url => url.replace(/[?#].*$/, '');
```
```js