DEV Community

Sh Raj
Sh Raj

Posted on

Remove query string from URL - Get URL Path - JavaScript

Below function may be useful for you for removing query string from URL and getting the URL Path only using JavaScript

function getPathFromUrl(url) {
  return url.split(/[?#]/)[0];
}
Enter fullscreen mode Exit fullscreen mode

or Minified Version

let getPathFromUrl =t=>t.split(/[?#]/)[0];
Enter fullscreen mode Exit fullscreen mode

Example :-

let getPathFromUrl =t=>t.split(/[?#]/)[0];

var url = 'https://pngaaaclone.sh20raj.repl.co/?q=wallpaper&page=2?q=Nba%20Logo';
getPathFromUrl(url) //Output :- 'https://pngaaaclone.sh20raj.repl.co/' ;
Enter fullscreen mode Exit fullscreen mode

Checkout a new WebScrapperJS + WhollyAPI Project

GitHub logo SH20RAJ / pngaaaclone

Pngaaa Clone Using WebScrapperJS and WhollyAPI

Demo :- https://pngaaaclone.sh20raj.repl.co/

Image description

Top comments (0)