I am having some trouble with Regex. Basically what I want to do is take any either of these types of spotify urls for a playlist
or
spotify:user:spotify:playlist:37i9dQZF1DZ06evO2ZpGiQ
And get the user id which would be spotify
for these URLs and would like to get the playlist id which would be 37i9dQZF1DZ06evO2ZpGiQ
for these URLs
I also would like to have some regex that would make sure it is either one of these URL types.
So far I have tried this regex to check if it is a spotify url using this regex:
^(spotify:|https:\/\/[a-z]+\.spotify\\/)
but it only matches the top URL.
I am having some trouble with Regex. Basically what I want to do is take any either of these types of spotify urls for a playlist
https://open.spotify./user/spotify/playlist/37i9dQZF1DZ06evO2ZpGiQ?si=-6S0EBlURrao__YyIOW6bg
or
spotify:user:spotify:playlist:37i9dQZF1DZ06evO2ZpGiQ
And get the user id which would be spotify
for these URLs and would like to get the playlist id which would be 37i9dQZF1DZ06evO2ZpGiQ
for these URLs
I also would like to have some regex that would make sure it is either one of these URL types.
So far I have tried this regex to check if it is a spotify url using this regex:
^(spotify:|https:\/\/[a-z]+\.spotify\.\/)
but it only matches the top URL.
3 Answers
Reset to default 2Here I get the immutable parts of each candidate strings which are https://open.spotify./user/spotify/playlist
and spotify:user:spotify:playlist
in a first group then I capture the playlist id in a second group and the rest can be ignored.
You can see it at work there: https://regex101./r/tDtsTS/1
^(https:\/\/open.spotify.\/user\/spotify\/playlist\/|spotify:user:spotify:playlist:)([a-zA-Z0-9]+)(.*)$
For your given examples you could also match the user id and the playlist id using an alternation |
for only the first part where you do not yet want to capture values and capture the separator in a capturing group ([\/:])
. Then you could use a backreference \1
Then you can refer to the same group number for the username and the playlist. The username will be in group 2 and the playlist in group 3.
^(?:https:\/\/open\.spotify\.|spotify)([\/:])user\1([^\/]+)\1playlist\1([a-z0-9]+)
Details
^
Assert position at the start of the line(?:
Non capturing grouphttps:\/\/open\.spotify\.
Match literally (escape the dot\.
to match it literally)|
Orspotify
match literally
)
Close non capturing group([\/:])
Capture/
or:
in a group using a character class so it can be used with a backreference to this group (group 1)user\1
Matchuser
followed by the backreference\1
([^\/]+)
Capture the user id (group 2) using a negated character class to match not a forward slash\1playlist\1
match playlist using 2 backreferences (like:playlist:
or/playlist/
)([a-z0-9]+)
Capturing the playlist id (group 3)
Regex demo
You could use the case insensitive flag /i
to match upper and lowercase characters.
const strings = [
'https://open.spotify./user/spotify/playlist/37i9dQZF1DZ06evO2ZpGiQ?si=-6S0EBlURrao__YyIOW6bg',
'spotify:user:spotify:playlist:37i9dQZF1DZ06evO2ZpGiQ'
];
let regex = /^(?:https:\/\/open\.spotify\.|spotify)([\/:])user\1([^\/]+)\1playlist\1([a-z0-9]+)/gi;
strings.forEach((str) => {
while ((m = regex.exec(str)) !== null) {
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
console.log("user id: " + m[2]);
console.log("playlist id: " + m[3]);
}
});
This is fairly easy, just use character classes.
Working example: https://regex101./r/eQFWuR/1
Regex spotify[\/:]playlist[\/:](.+)[\s?]
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745627157a4636878.html
评论列表(0条)