javascript - Using Regex to parse a URI -


i'm using modenizr determine link serve users based on device of choice. if they're using mobile device want return uri if not return traditional url.

uri: spotify:album:1jcywzsn7jeve9xsq9buux

url: https://open.spotify.com/album/1jcywzsn7jeve9xsq9buux

right i'm using slice() retrieve last 22 characters of uri. though works i'd parse string via regex in event uri exceeds aforementioned character amount. best way string of characters after second colon of uri?

$(".spotify").attr("href", function(index, value) {   if (modernizr.touch) {     return value   } else {     return "https://open.spotify.com/album/" + value.slice(-22);   } }); 

regex appropriate task because quite simple, here's regex supports many : there , still work

/[\w\:]*\:(\w+)/ 

how works

[\w\:]* word characters (letters, numbers, underscore) , colons

\: tell previous thing stop @ colon. regex default greedy, means last colon

(\w+) select word characters , store in group can access it


use like:

var string = 'spotify:album:1jcywzsn7jeve9xsq9buux',     parseduri = string.match(/[\w\:]*\:(\w+)/)[1]; 

parseduri result


and can combine this:

var url = 'https://open.spotify.com/album/'+parseduri; 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -