How to Extract Path and Parameters from a URL in JavaScript
You can use the 'URL' object in JavaScript to parse the URL and extract the desired path and parameters. Here's an example of how to achieve this:
const urlString = 'https://github.com/test%3Efrew?ew332432=321423';
const url = new URL(urlString);
const pathWithParams = url.pathname + url.search;
console.log(pathWithParams); // Output: /test%3Efrew?ew332432=321423
In the above code, we create a new 'URL' object with the given URL string. Then, we concatenate the 'pathname' (which represents the path portion of the URL) with the 'search' (which represents the query parameters) to get the desired result.
原文地址: https://www.cveoy.top/t/topic/pvNX 著作权归作者所有。请勿转载和采集!