Home > database >  Regex matched the link with only one sub folder and requested content is JavaScript file
Regex matched the link with only one sub folder and requested content is JavaScript file

Time:07-02

I'm writing a regex matching the links have only one sub folder with the requested content is JavaScript files with the length of 7 characters or more and no ? and & character in the back of the file.

Some specific examples:

(1) https://some.domain.example.tld/randomcharacter/randomfilename.js // match
(2) https://some.domain.example.tld/randomcharacter/randomfilename.js? // not match
(3) https://some.domain.example.tld/randomcharacter/randomfilename.js?a=b&c=d // not match
(4) https://some.domain.example.tld/subdir/public/lib/randomcharacter/randomfilename.js // not match

P/s: Please understand tld, randomcharacter and randomfilename are non-fixed.

This is my current regex:

/\/[a-zA-Z]{7,}\/[a-zA-Z]{7,}\.js$(?!(\?|&))/

It is correct for cases (1), (2) and (3). In the case (4), although there are a few other paths in front, it still matches and I don't want that! enter image description here

How do I make it match exactly as I want it to be?

CodePudding user response:

For that, you need to include the first '//' to guarantee that you will have only 1 subfolder like:

\/\/[a-zA-Z.] \/[a-zA-Z]{7,}\/[a-zA-Z]{7,}\.js$

And you don't need the (?!(?|&)) after the end of the line '$' since there is nothing if the '$' matches

  • Related