Translate this JavaScript Gibberish please? [closed]

∥☆過路亽.° 提交于 2021-01-28 06:32:10

问题


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;

回答1:


Alright, you asked for it :)

Start the regular expression:

/

Start a group operation:

(

Search for the text "obidos" followed by any single character

obidos.

Open another group operator:

(

Search for the text "ASIN" followed by any 12 characters

ASIN.{12}

Another group operation:

(

Followed by 0 or more characters that are not slashes:

[^\/]*

Group operation searching for an '=' character or a url encoded '=' (%3D):

(=|%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:

redirect[^\/]*

Matches any single character, followed optionally by the text "tag=":

.(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:

/




回答2:


Download a copy of expresso, its a great utility for this and comes in handy for all this stuff. then just place the regex into that (everything between the starting slashes and ending slash).

I would describe what string it matches e.c.t. but its fairly complex as theres lots of components to it. Its easier for you to look at it yourself. expresso provides a more english explanation of each pattern



来源:https://stackoverflow.com/questions/10621334/translate-this-javascript-gibberish-please

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!