Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate SoundCloud URL via javascript regex

I have 2 urls from soundcloud that I would like to get everything after the .com/(or).sc/, I don't understand regex at all, I could do the javascript match/replace, only problem is the http vs https

https://soundcloud.com/ilyao/lyao-lj-mtx-and-lj-mtxs-hair

http://snd.sc/1cvgIjv

Any help would be much appreciated.

Here's what I've got, not sure how to do the https of the replace or if this is the best way to approach this...

function getSoundCloudInfo(url){
    return url.replace("https://soundcloud.com/", "").replace("http://snd.sc/", "");
}
like image 297
Brian Putt Avatar asked Aug 14 '13 08:08

Brian Putt


3 Answers

function getSoundCloudInfo(url){
  var regexp = /^https?:\/\/(soundcloud\.com|snd\.sc)\/(.*)$/;
  return url.match(regexp) && url.match(regexp)[2]
}
like image 56
Andrea Salicetti Avatar answered Nov 06 '22 11:11

Andrea Salicetti


It is too late, but...

Don't forget about:

  1. mobile version (m. after http(s)://)
  2. possible slash at the end
  3. with or without SSL/TLS, so it can be http or https
  4. with or without www

So, full regex is:

^(https?:\/\/)?(www.)?(m\.)?soundcloud\.com\/[\w\-\.]+(\/)+[\w\-\.]+/?$

like image 6
Mark Mishyn Avatar answered Nov 06 '22 13:11

Mark Mishyn


((https:\/\/)|(http:\/\/)|(www.)|(m\.)|(\s))+(soundcloud.com\/)+[a-zA-Z0-9\-\.]+(\/)+[a-zA-Z0-9\-\.]+    
like image 4
Jithin U. Ahmed Avatar answered Nov 06 '22 13:11

Jithin U. Ahmed