I’m trying to modify and update an old Greasemonkey script with the goal of automatically adding an affiliate ID to all Amazon links. I’m a novice when it comes to JavaScript, but I’m usually pretty good about modifying existing scripts in any language. There’s just one line here that I can’t wrap my head around.
The script I started with is outdated, so I don’t know if there is a problem with the syntax or if the link format has changed. Can somebody please help me understand what this line is doing so I can make changes to it?
const affiliateLink = /(obidos.(ASIN.{12}([^\/]*(=|%3D)[^\/]*\/)*|redirect[^\/]*.(tag=)?))[^\/&]+/i;
Alright, you asked for it 🙂
Start the regular expression:
Start a group operation:
Search for the text “obidos” followed by any single character
Open another group operator:
Search for the text “ASIN” followed by any 12 characters
Another group operation:
Followed by 0 or more characters that are not slashes:
Group operation searching for an ‘=’ character or a url encoded ‘=’ (%3D):
Followed by 0 or more characters that are not slashes:
Followed by slash (and closes the current group), which can be repeated 0 or more times:
Allows the pattern to match if the previous group was found OR everything to the right of the bar is matched:
Matches the text “redirect” followed by 0 or more chatacters that are not a slash:
Matches any single character, followed optionally by the text “tag=”:
Closes the two group operations we’re currently still inside of:
Followed by one or more characters that are not a slash or &:
Closes the regular expression: