JavaScript RegExp排除特定的文件扩展名和index.js

时间:2021-12-06 03:41:12

This is my current RegExp:

这是我现在的RegExp:

/^(?!index).*js/gmi

This is the list the RegExp is applied to:

这是RegExp应用于的列表:

test/test.js
hey/test-bundle.js
test/test-heybundle.js
test/index.js
test/indop.js
lollipop/testindex.js
test/test.scss
test/test.css
lalilu/hey.yml
test.js

What it should do:

它所应该做的是:

  • Only match files ending with *.js
  • 只匹配以*.js结尾的文件
  • Exclude the filename index.js
  • 排除文件名index.js
  • Exclude files ending with "-bundle.js"
  • 排除以“-bundl .js”结尾的文件
  • Only match files that are located in a directory (e.g. /test/test.js, not test.js)
  • 只匹配位于目录中的文件(例如/test/test)。js,而不是. js)

I'd really much appreciate any help to get this RegExp to work like expected.

非常感谢您的帮助,让这个RegExp像预期的那样工作。

1 个解决方案

#1


3  

/^[a-z]+\/(?!index\.js$)[a-z]+(?!-bundle)(?:-[a-z]+)?\.js$/gm

/ ^[a - z]+ \ /(? !美元指数\ . js)[a - z]+(? !包)(:-[a - z]+)? \ . js /通用美元

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

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

^[a-z]+\/ - Match a parent directory (assuming the dir separator will always be a /).

^[a - z]+ \ / -匹配一个父目录(假定dir分离器将永远是一个/)。

(?!index\.js$) - Fail the match if the parent dir is followed by index.js and the end of the line.

(?!index\.js$) -如果父目录后面跟着索引,则失败匹配。和线的末端。

[a-z]+(?!-bundle) - One or more letters not followed by -bundle.

[a-z]+(?!-bundle) -一个或多个字母后面不跟着-bundle。

(?:-[a-z]+)? - Optional 2nd part of the file name following a hyphen. Change the ? to a * if you also have files that contain more than 2 hyphenated sections.

(:-[a - z]+)?-可选在连字符后的文件名的第二部分。改变吗?如果您还有包含两个以上的连字符段的文件,请发送到a *。

\.js$ - File extension and end of line.

\。文件扩展名和行尾。

#1


3  

/^[a-z]+\/(?!index\.js$)[a-z]+(?!-bundle)(?:-[a-z]+)?\.js$/gm

/ ^[a - z]+ \ /(? !美元指数\ . js)[a - z]+(? !包)(:-[a - z]+)? \ . js /通用美元

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

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

^[a-z]+\/ - Match a parent directory (assuming the dir separator will always be a /).

^[a - z]+ \ / -匹配一个父目录(假定dir分离器将永远是一个/)。

(?!index\.js$) - Fail the match if the parent dir is followed by index.js and the end of the line.

(?!index\.js$) -如果父目录后面跟着索引,则失败匹配。和线的末端。

[a-z]+(?!-bundle) - One or more letters not followed by -bundle.

[a-z]+(?!-bundle) -一个或多个字母后面不跟着-bundle。

(?:-[a-z]+)? - Optional 2nd part of the file name following a hyphen. Change the ? to a * if you also have files that contain more than 2 hyphenated sections.

(:-[a - z]+)?-可选在连字符后的文件名的第二部分。改变吗?如果您还有包含两个以上的连字符段的文件,请发送到a *。

\.js$ - File extension and end of line.

\。文件扩展名和行尾。