I have a form input where users enter a valid URL. My goal is to extract the URL, query string (if provided by the user), and hash value (if provided) separately.
This is what I currently have:
var url = userInput.split('?')[0]; //get url
var qs = userInput.split('?')[1].split('#')[0]; //get query string
var hash = userInput.split('#')[1]; //get hash value
The issue arises when the user enters only a hash value without a query string.
http://example.com/some-page/ref?param=value#4567
Expectations are as follows:
URL: http://example.com/some-page/ref
Query String: ?param=value
Hash Value: #4567