最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Validate SoundCloud URL via javascript regex - Stack Overflow

programmeradmin0浏览0评论

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




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("/", "").replace("/", "");
}

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/", "");
}
Share Improve this question edited Aug 14, 2013 at 9:34 Brian Putt asked Aug 14, 2013 at 8:48 Brian PuttBrian Putt 1,3484 gold badges16 silver badges33 bronze badges 2
  • Could you show us what you have tried ? – HamZa Commented Aug 14, 2013 at 9:24
  • 1 @HamZa I just added what I'm currently using – Brian Putt Commented Aug 14, 2013 at 9:34
Add a comment  | 

4 Answers 4

Reset to default 7
function getSoundCloudInfo(url){
  var regexp = /^https?:\/\/(soundcloud\.com|snd\.sc)\/(.*)$/;
  return url.match(regexp) && url.match(regexp)[2]
}

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\-\.]+/?$

((https:\/\/)|(http:\/\/)|(www.)|(m\.)|(\s))+(soundcloud.com\/)+[a-zA-Z0-9\-\.]+(\/)+[a-zA-Z0-9\-\.]+    
const FULL_YOUTUBE_REG_EXP = /^(?:(https?):\/\/)?(?:(?:www|m)\.)?youtube\.com\/watch.*v=([a-zA-Z0-9_-]+)/
const SHORT_YOUTUBE_REG_EXP = /^(?:(https?):\/\/)?(?:(?:www|m)\.)?youtu\.be\/([a-zA-Z0-9_-]+)/
const VIMEO_REG_EXP = /^(?:(https?):\/\/)?(?:www\.)?vimeo\.com\/(\d+)/
const SOUNDCLOUD_REGEXP = /^(?:(https?):\/\/)?(?:(?:www|m)\.)?(soundcloud\.com|snd\.sc)\/(.*)$/
发布评论

评论列表(0)

  1. 暂无评论