I am trying to match viable package.json files to get all modules in a directory, including nested ones. I ran into an issue where someone has a two package.json files in their module. One is in the root and one is nested. I'd like to only match the regex with the first line and not the second. Do I have to parse the strings into arrays with path.sep or can I do this solely with regex?
/Users/thomas/Desktop/exmaple/node_modules/stream-http/package.json
/Users/thomas/Desktop/exmaple/node_modules/stream-http/test/browser/package.json
Pattern
^.+\/node_modules\/.+\/package.json$
https://regexr.com/45qvr
Using .+ greedily catches any characters, so it will always match as much as possible to find a package.json at the end. If you use [^\/]+ instead, it will only match characters that are not /, making sure that the package.json is only matched after exactly one directory under the node_modules/.
^.+\/node_modules\/[^\/]+\/package.json$
Related
I have been stuck at a code and unable to find the meaning of /** common to package.json files field.
E.g. here
"files":[
"./*",
"./**",
"icon.*"
]
I am unable to know the meaning of any of the element of files in the above code taken from package.json
It's directories wildcards:
./* - Any file in current directory.
./** - Any file in current directory and sub directories.
icon.* - Any file with name icon.
Read more about package.json from NPM documentation
Those are known as globs, see https://www.npmjs.com/package/glob for instance:
* Matches 0 or more characters in a single path portion
** If a "globstar" is alone in a path portion, then it matches zero or more directories and subdirectories searching for matches. It does not crawl symlinked directories.
Not exactly sure who created them in the very first place though to be honest.
EDIT: actually, from Wiki:
The glob command, short for global, originates in the earliest versions of Bell Labs' Unix.
my Package.bundle reads
var reqContext = require.context('./', true, /\.js$/);
reqContext.keys().map(reqContext);
Which basically includes all .js files.
I want the expression to exclude any ***.spec.js files . Any regexp here to exclude .spec.js files ?
Since /\.js$/ allows all .js files (as it basically matches .js at the end of the string), and you need to allow all .js files with no .spec before them, you need a regex with a negative lookahead:
/^(?!.*\.spec\.js$).*\.js$/
See this regex demo
Details:
^ - start of string
(?!.*\.spec\.js$) - the line cannot end with .spec.js, if it does, no match will occur
.* - any 0+ chars other than linebreak symbols
\.js - .js sequence
$ - end of the string.
Although the accepted answer is technically correct, you shouldn't need to add this regex to your webpack configuration.
This is because webpack only compiles the files that are referenced via require or import and so your spec.js files won't be included in the bundle as they are not imported anywhere in the actual code.
I saw this pattern used in a configuration file for protractor.
specs: [
'test/e2e/**/*.spec.js'
]
To mean it means "all files inside test/e2e". What kind of pattern is this? I think it's not regex because of those unescaped slashes. Especially, why is there ** in the middle, not just test/e2e/*.spec.js?
I tried using the search engine, but did not find anything useful probably because the asterisks don't work very well in search engines.
What kind of pattern is this?
It is called "glob". The module glob is a popular implementation for Node, and appears to be the one used by Protractor.
Especially, why is there "**" in the middle, not just "test/e2e/*.spec.js"?
** means it can match sub-directories. It's like a wildcard for sub-directories.
For example, test/e2e/*.spec.js would match test/e2e/example.spec.js, but not test/e2e/subdir/example.spec.js. However test/e2e/**/*.spec.js matches both.
It is called "glob" syntax. Glob is a tool which allows files to be specified using a series of wildcards.
*.js means "everything in a folder with a js extension.
** means "descendant files/folders.
**/*.js means "descendant files with a js extension in descendant folders."
test/e2e/**/*.spec.js' means the above, starting in the test/e2e/ directory.
So, for example, given this file system:
test/e2e/foo/a.spec.js <-- matches
test/e2e/foo/butter.js <-- does not include "spec.js"
test/e2e/bar/something.spec.js <-- matches
test/other/something-different.spec.js <-- not in /test/e2e
The final pattern would match:
test/e2e/foo/a.spec.js
test/e2e/bar/something.spec.js
It's a globbing pattern. Most javascript things using globbing patterns seem to be based around the glob npm package. It's worth taking a look at the documentation as there are some handy hints in there for when you have more complex situations.
The path you are asking about will match any file ending .spec.js in any subdirectory under test/e2e
I am using RequireJS optimizer in a gulp recipe to compile and concatenate my Modules but redundant 3rd party library files like bower.json and *.nuspec files are being copied to my output directory.
I have successfully managed to exclude full directories using fileExclusionRegExp in the requirejs.optimize options object with the following expression:
/^\.|^styles$|^templates$|^tests$|^webdriver$/
However, I cannot figure out how to exclude everything but .js file extensions. I could use the following:
/^\.|.json$|.nuspec$|^styles$|^templates$|^tests$|^webdriver$/
to exclude specific extensions but if a new type were to appear later, I would have to notice and then change the regex. Also, the regex would probably become unruly and hard to maintain with time. I have tried to use the following expressions:
/^\.|!js$|^styles$|^templates$|^tests$|^webdriver$/
/^\.|!.js$|^styles$|^templates$|^tests$|^webdriver$/
/^\.|^.js$|^styles$|^templates$|^tests$|^webdriver$/
/^\.|[^.js$]|^styles$|^templates$|^tests$|^webdriver$/
/^\.|[^.js]$|^styles$|^templates$|^tests$|^webdriver$/
The results ranged from doing nothing (the first 3, to breaking the build, last 2) any help anyone could provide would be appreciated.
Thanks
Try this regex:
^\.|\.(json|nuspec)$|^(styles|templates|tests|webdriver)$
I'm deploy my webapp at a windows maschine (non POSIX command line) and I'm generating JS metric with Plato.
My Question is how can I exclude all files from a given folder that contains subfolders via regular expressions with plato.js.
I tried this command for excluding all minified JS Libs:
$ plato -x "^js[a-zA-Z0-9-.\/]*.?js" -r -d report src/app/
All JS files of src/app/js/**/*.js are not excluded.
I test my regex with rubular: http://rubular.com/r/zbTsv1nIWY (fix underscore issue is optional)
Can somebody help me please?
Remember to escape (.) because you want to match it literally.
i dunno why have you left out the _ (underscore) but an optional solution can be something like this :
^js\/(.*)\.js$
folder name starting with js and file name ending with .js
and as you are saying another solution can be to just include the _
^js[a-zA-Z0-9-\.\/_]*.?js
demo here : http://rubular.com/r/DPqnxtDB6E