jskidd3
jskidd3

Reputation: 4783

JavaScript split window.location.hash

var fullHash = window.location.hash;
// fullHash = #search?t=operator&q=Velvet

I need to split the fullHash variable into 3 separate values. One value needs to say 'search', another should say 'operator', and the last should say 'Velvet'. I have tried multiple regex style splits but can't figure it out. If anyone could help me write this I would highly appreciate it!

Upvotes: 0

Views: 562

Answers (3)

falsetru
falsetru

Reputation: 369394

var fullHash = '#search?t=operator&q=Velvet';
fullHash.match(/[#=][^#=?&]*/g).map(function(m) {
    return m.substr(1);
})
// => ["search", "operator", "Velvet"]

Upvotes: 0

Amit Joki
Amit Joki

Reputation: 59282

Simple solution:

var fullHash = window.location.hash;
    fullHash.match(/\w{3,}/g);

Results in:

["search", "operator", "Velvet"]

Upvotes: 3

Rafa Paez
Rafa Paez

Reputation: 4860

You can use this regex (demo)

#(\w+)\?t=(\w+)&q=(\w+)

Upvotes: 1

Related Questions