Marc Sloth Eastman
Marc Sloth Eastman

Reputation: 801

Why is my JS regex matching more than it should?

I am trying to match a string like "https://open.spotify.com/track/1c3v8Ww4q8Vah92JBxAzxi?si=oqw6SQtxTVqJuoe2C_XrzQ omg this ones nice" and extract the part after the last slash, in this case "1c3v8Ww4q8Vah92JBxAzxi?si=oqw6SQtxTVqJuoe2C_XrzQ". Could anyone help me figure out the regex with appropriate group for this? I already tried the following:

/.*spotify\.com.*/
/.*spotify\.com.\/track\/.*/
/.*spotify\.com.\/track.*/
/.*spotify\.com\.\/track.*/
/.*spotify\.com\.\/track\/.*/
/.*spotify\.com\..*/
/.*spotify\.com\/.*/
/.*spotify\.com\/track\/.*/
/.*spotify\.com\/track\/(.*)/
/.*spotify\.com\/track\/(.*)\s*/
/.*spotify\.com\/track\/(.*)\s*$/
/.*spotify\.com\/track\/(\S)*/
/.*?spotify\.com\/track\/(\S)*/
/\/(.*)?\s/

but it normally ends up matching the whole link like "https://open.spotify.com/track/1c3v8Ww4q8Vah92JBxAzxi?si=oqw6SQtxTVqJuoe2C_XrzQ"

Upvotes: 1

Views: 60

Answers (2)

user4399354
user4399354

Reputation:

Using \w will help you a lot here. It matches any letter, digit or underscore. Equivalent to [a-zA-Z0-9_].

track = "https://open.spotify.com/track/1c3v8Ww4q8Vah92JBxAzxi?si=oqw6SQtxTVqJuoe2C_XrzQ omg this ones nice";
re = /track\/(\w+\?\w+=\w+)\s/;

match = track.match(re);
match[1];  # 1st parenthesized group: "1c3v8Ww4q8Vah92JBxAzxi?si=oqw6SQtxTVqJuoe2C_XrzQ"
match[0];  # the entire match: "track/1c3v8Ww4q8Vah92JBxAzxi?si=oqw6SQtxTVqJuoe2C_XrzQ "

Upvotes: 0

xate
xate

Reputation: 6379

Maybe something like:

\/track\/([^ ]*)

https://regex101.com/r/PwRXlA/1

  1. search for literal /track/
  2. [^ ]* match everything until whitespace

as you tagged js:

'https://open.spotify.com/track/1c3v8Ww4q8Vah92JBxAzxi?si=oqw6SQtxTVqJuoe2C_XrzQ omg this ones nice'.match(/\/track\/([^ ]*)/)[1]

Output

"1c3v8Ww4q8Vah92JBxAzxi?si=oqw6SQtxTVqJuoe2C_XrzQ"

Upvotes: 4

Related Questions