阅读背景:

通过javascript regex验证SoundCloud URL

来源:互联网 

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

我有2个来自soundcloud的网址,我想在.com /(或).sc /后得到一切,我根本不懂正则表达式,我可以做javascript匹配/替换,唯一的问题是http vs https

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

https://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...

这是我得到的,不知道如何做替换的https或如果这是最好的方法来解决这个问题...

function getSoundCloudInfo(url){
    return url.replace("https://soundcloud.com/", "").replace("https://snd.sc/", "");
}

3 个解决方案

#1


5  

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

#2


4  

((https:\/\/)|(http:\/\/)|(www.)|(m\.)|(\s))+(soundcloud.com\/)+[a-zA-Z0-9\-\.]+(\/)+[a-zA-Z0-9\-\.]+    

#3


4  

It is too late, but...

现在为时已晚,但......

Don't forget about:

别忘了:

  1. mobile version (m. after http(s)://)
  2. 移动版(m。在http(s)之后://)

  3. possible slash at the end
  4. 最后可能削减

  5. with or without SSL/TLS, so it can be http or https
  6. 有或没有SSL / TLS,所以它可以是http或https

  7. with or without www
  8. 有或没有www

So, full regex is:

所以,完整的正则表达式是:

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


分享到: