Regex to validate URL hash not working in Javascript

I have the following code for checking the URL hash part and extract the query strings.

var getCleanHash = function(){
    return (/^[a-z-_&=\d]*$/.test(location.hash.substr(1))) ? location.hash.substr(1) : "";
}

so the regex used here is /^[a-z-_&=\d]*$/, but if I have the query strings like type=mytype&q=search, it is returning empty value. If I remove the ^ from the regex, it starts returning the query strings. But then it will not perform any checks as intended in regex. I suppose ^ denotes the first character, but not entirely sure why it doesn't match with my query string part.

Can someone help me to find what this Regex actually means and how can I fix the issue with empty query strings?



Read more here: https://stackoverflow.com/questions/67935276/regex-to-validate-url-hash-not-working-in-javascript

Content Attribution

This content was originally published by Happy Coder at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: