Skip to content

Instantly share code, notes, and snippets.

@thejsa
Created October 21, 2018 13:32
Show Gist options
  • Save thejsa/b34edc014f3a4d616f155185caeedc1d to your computer and use it in GitHub Desktop.
Save thejsa/b34edc014f3a4d616f155185caeedc1d to your computer and use it in GitHub Desktop.
JavaScript function + regular expression to parse Spotify URIs
function parseURI(uri) {
var parse;
if(!(parse = /^(?:https?:\/\/open.spotify.com\/|spotify:)(track|album|artist|user|playlist)(?:\/|:)([a-zA-Z0-9_]+)(?:(?:\/|:)(playlist)(?:\/|:)([a-zA-Z0-9_]+))?(.*)$/.exec(uri))) {
return false;
}
var result = {
uri: parse[0],
raw: parse
};
switch(parse[1]) {
case 'track':
case 'album':
case 'artist':
case 'playlist':
result.type = parse[1];
result.id = parse[2];
break;
case 'user':
if(parse[3] == 'playlist') {
result.type = parse[3];
result.id = parse[4];
result.user = parse[2];
} else {
result.type = parse[1];
result.id = parse[2];
}
break;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment