diff --git a/snippets/getBaseURL.md b/snippets/getBaseURL.md index 984cfbf9c..2f6dfb76b 100644 --- a/snippets/getBaseURL.md +++ b/snippets/getBaseURL.md @@ -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