简体   繁体   中英

Regular Expression for multiple folder names in URL and Excluded Extension Flags

I'm processing multiple js files to build bundles via webpack. However I need to target multiple js files to process across multiple folders, while being able to exclude js files with certain flags in the filename.

I have the following files:

/styles/portal/version2/libraries/jquery.js
/styles/portal/version2/libraries/library.exclude.js
/styles/portal/version2/components/componenta.js
/styles/portal/version2/konstructs/konstructa.js
/styles/portal/version2/konstructs/konstructb.js
/styles/portal/version2/konstructs/konstructc.js
/styles/portal/version2/konstructs/konstructd.disabled.js
/styles/portal/version2/vendors/vendor.js
/styles/portal/version2/vendors/vendor.exclude.js

And this should only grab the following files to bundle:

/styles/portal/version2/libraries/jquery.js

/styles/portal/version2/konstructs/konstructa.js
/styles/portal/version2/konstructs/konstructb.js
/styles/portal/version2/konstructs/konstructc.js

/styles/portal/version2/vendors/vendor.js

So essentially any.js file out of the specific libraries , konstructs , vendor folders and then don't include anything with disabled.js or exclude.js

To get the three directories libraries, konstructs or vendors that contain
non- disabled nor exclude in the file name parts of .js files would be this:

/^.*?\/(?:libraries|konstructs|vendors)(?=\/)(?:[^\/.]*\/)*(??[^\/]*?\b(:?disabled|exclude)(.=\?)[^\/]*.\?js$)[^\/]*.\.js$/gm

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

Note, use the multi-line modifier if looking at multi-line strings.
If not, no need for it.

You can try exclude option in rule-condition with this pattern

/\.(exclude|disabled)\.js$/
  • \. - Matches .
  • (exclude|disabled) - Match exclude or disabled word
  • \.js - Matches . followed by js . followed by js
  • $ - End of string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM