Reputation: 49
I just want to match my url with this URL all possible combinations like www without www, http,https
Basically any input URL that matches with soundcloud music url like
https://soundcloud.com/mazz-ika-free/wegz-molotof-dorak-gai-2020
FIRST PART:- https://soundcloud.com/ (http,https)
SECOND PART :- Alphanumeric with only '-' allowed.
THIRD PART :- Aphanumeric with only '-' allowed
I have tried this regex so far :
/^https?:\/\/(soundcloud\.com|snd\.sc)\/(.*)$/
Please help
Upvotes: 3
Views: 775
Reputation: 555
According to SoundCloud the following rules apply to links (everything after soundcloud.com):
I've written a regex pattern for the entire link:
^(?:https?:\/\/)((?:www\.)|(?:m\.))?soundcloud\.com\/[a-z0-9](?!.*?(-|_){2})[\w-]{1,23}[a-z0-9](?:\/.+)?$
Here's a version without all the non-capturing groups:
^https?:\/\/(www\.|m\.)?soundcloud\.com\/[a-z0-9](?!.*?(-|_){2})[\w-]{1,23}[a-z0-9](?:\/.+)?$
Test it out here: https://regex101.com/r/OWbj5s/5
Upvotes: 1
Reputation: 163362
You could match either soundcloud.com or snd.sc followed by a repeating pattern matching a /
followed by 1+ word chars.
The word chars part by itself also has a repeating part matching a -
and 1+ word chars.
If you only want to match alphanumeric, you could use [a-zA-Z0-9]
instead of \w
which could also match an _
^https?:\/\/(?:soundcloud\.com|snd\.sc)(?:\/\w+(?:-\w+)*)+$
Explanation
^https?:\/\/(?:soundcloud\.com|snd\.sc)
Match the protocol and either soundcloud or snd(?:
Non capture group
\/\w+
Match /
and 1+ word chars(?:-\w+)*
Repeat 0+ times matching -
and 1+ word chars)+
Close group and repeat 1+ times to match at least a single /
part$
End of stringUpvotes: 1
Reputation: 521467
One option would be to create a new anchor <a>
element from JavaScript, assign the URL to the href
attribute, and then check the host
property of that anchor:
function checkURL(input) {
var url = document.createElement('a');
url.href = input;
return url.host === "soundcloud.com" || url.host === "snd.sc";
}
console.log(checkURL("https://soundcloud.com/mazzikafree/wegz-molotof-dorak-gai-2020"));
console.log(checkURL("https://snd.sc/mazzikafree/wegz-molotof-dorak-gai-2020"));
console.log(checkURL("http://www.google.com"));
Here, you would be checking that the host
is either soundcloud.com
or snd.sc
.
Upvotes: 0
Reputation: 444
Try this:
var re = /^(https?:\/\/)?(www\.)?(soundcloud\.com|snd\.sc)\/.*$/
I've tested following:
If you require http specifier, then consider using
var re = /^https?:\/\/(www\.)?(soundcloud\.com|snd\.sc)\/.*$/
Upvotes: 0
Reputation: 439
I think you might have forgotten the backslash
before the normal slash
char.
Try this:
^https?:\/\/(soundcloud.com|snd.sc)\/(.*)$
Upvotes: 0