Update getBaseURL.md
This commit is contained in:
@ -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
|
||||
|
||||
Reference in New Issue
Block a user