最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Spotify urls regex - Stack Overflow

matteradmin7PV0评论

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.

Share Improve this question edited Jun 15, 2018 at 13:46 Fatal Dizz asked Jun 15, 2018 at 13:42 Fatal DizzFatal Dizz 571 silver badge8 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 2

Here 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 group
    • https:\/\/open\.spotify\. Match literally (escape the dot \. to match it literally)
    • | Or
    • spotify 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 Match user 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?]

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far