Validating SoundCloud URL with javascript regex

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

https://soundcloud.com/ilyao/lyao-lj-mtx-and-lj-mtxs-hair http://snd.sc/1cvgIjv 

Any help would be greatly appreciated.

Here is what I have, I donโ€™t know how to make https replacements, or if this is the best way to approach this ...

 function getSoundCloudInfo(url){ return url.replace("https://soundcloud.com/", "").replace("http://snd.sc/", ""); } 
+4
source share
3 answers
 function getSoundCloudInfo(url){ var regexp = /^https?:\/\/(soundcloud\.com|snd\.sc)\/(.*)$/; return url.match(regexp) && url.match(regexp)[2] } 
+5
source
 ((https:\/\/)|(http:\/\/)|(www.)|(m\.)|(\s))+(soundcloud.com\/)+[a-zA-Z0-9\-\.]+(\/)+[a-zA-Z0-9\-\.]+ 
+3
source

Too late, but ...

Do not forget about:

  • Mobile version ( m. After http (s): //)
  • possible slash at the end
  • with or without SSL/TLS , so it can be http or https
  • with or without www

So the full regex:

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

+3
source

Source: https://habr.com/ru/post/1496983/


All Articles