I have a folder structure like this:
main
|__Test
|__Sub
|__Match
|__Match1
|__Match2
I have a requirement to:
Match only one folder - "Match"
Match multiple folder - "Match, Match1, Match2
For a) I've tried:
/main\/Test\/Sub\/Match\//
But that didn't help.
Basically, I am using Isparta, and I need to exclude a few folders from instrumentation.
/^main\/Test\/Sub\/Match[0-9]?\/?$/
I used [0-9] because you mentioned a structure Like. Assume there are not only match1 and match2.
Try this one:
\/main\/Test\/Sub\/Match([0-9])?\/?
Related
I can't find solution how to extract specific part of a path when I have a glob pattern for it.
I have incoming file paths that look like this:
'src/features/gui/images/main/effects/lightning/1.png'
And I want to extract specific leading part of it (always beginning of a path):
'src/features/gui/images/main/'
And I have two incoming glob patterns (the patterns may change, but will always point to a directories and images inside them):
'src/features/*/images/*/'
'src/features/*/images/*/**/*.{png,jpg,gif}'
I would like to extract directory path that matches pattern #1 in incoming file paths.
My intuition tells me it should be trivial, but can't find a solution. I'm looking in a solution that operates only on strings (or regexp) and do not do additional glob searches on disk. I've searched various JS modules on NPM, but all what I've found only returns if path matches glob pattern, but not actual match substring.
At least the first pattern is easy to translate to a regex (note that I'm assuming that the directory names in between contain only characters within the \w-metacharacter), which in turn allows you to easily obtain the required substring (in that case the capturing group at index 1):
const regex = /^.*(src\/features\/\w+\/images\/\w+\/).*$/gm;
const str = 'src/features/gui/images/main/effects/lightning/1.png';
const match = regex.exec(str)
console.log(match[1]); // prints src/features/gui/images/main/
I have a regex here. I'd like to know how to add multiple conditions, I have tried to use the pipe | but it does not seem to work as expected. I'm wanting to match files like:
Any assistance is appreciated.
file.jsx
mycool.file.jsx
mysupercoolfile.jsx
ignore /dist folder
I have tried to use the following regexs
/folder\/file\.jsx?$/
(/folder\/file\.jsx?|mycool.file)$/
Edit
I want to match the files:
folder/filename.jsx
fodler/test.filename.jsx
With the common pattern to be filename
You can use this regex to extract filename from file path.
(?m)[^/\\]+$
For Javascript:
/[^\/\\]+$/mg
This is my current RegExp:
/^(?!index).*js/gmi
This is the list the RegExp is applied to:
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
Exclude the filename index.js
Exclude files ending with "-bundle.js"
Only match files that are located in a directory (e.g. /test/test.js, not test.js)
I'd really much appreciate any help to get this RegExp to work like expected.
/^[a-z]+\/(?!index\.js$)[a-z]+(?!-bundle)(?:-[a-z]+)?\.js$/gm
https://regex101.com/r/Yqaajy/1
^[a-z]+\/ - Match a parent directory (assuming the dir separator will always be a /).
(?!index\.js$) - Fail the match if the parent dir is followed by index.js and the end of the line.
[a-z]+(?!-bundle) - One or more letters not followed by -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.
\.js$ - File extension and end of line.
I have a large project that has many *.html files and many *.tpl.html files.
I want to use a regular expression that allows me to differentiate between these two for my Webpack config.
I have tried using laziness to achieve this, like .*?\.html but this also matches *.tpl.html. https://regex101.com/r/a0fl4H/1
How can this be achieved?
Try this:
^(?!.*\.tpl).+\.html$
Demo:
https://regex101.com/r/a0fl4H/8
For regex, this should do it;
/.*?[^.tpl]\.html/
Working example
Edit: This first solution needs improvement. As mentioned in the comments, this will provide false positives for test.t.html - as it matches any of the given characters (.tpl).
This is a working version using;
^(?!.*\.tpl).*.html
bar.html // matches
bar.tpl.html // doesn't match
test.t.html // matches
test.p.html // matches
test.z.html // matches
I have a tree of components and inside of that tree are __magic_names__ folders (e. g. __tests__, __fixtures__, etc.). I want to filter out any files inside of these __magic_names__ folders. I'm using webpack and using require.context to slurp up my components - and I don't want my tests to ship with my production code.
I have the following regular expression, which is supposed to filter out every file path that contains a double-underscore. Unfortunately, it matches paths with __magic__ folders too:
^./((?!=__)[^/]+?(?!=__)/)*((?!=__)[^/]+?(?!=__)).jsx?$
Should work:
./SomeComponent.js
./SomeComponent.jsx
./SomeComponent/SomeComponent.js
./SomeComponent/SomeComponent.jsx
./SomeComponent/ChildComponent/Child.js
./SomeComponent/ChildComponent/Child.jsx
Should fail
./__magic__/SomeComponent.js
./__magic__/SomeComponent.jsx
./SomeComponent/__magic__/SomeComponent.js
./SomeComponent/__magic__/SomeComponent.jsx
./SomeComponent/__magic__/ChildComponent/Child.js
./SomeComponent/__magic__/ChildComponent/Child.jsx
./SomeComponent/ChildComponent/__magic__/Child.js
./SomeComponent/ChildComponent/__magic__/Child.jsx
Debuggex visualizes it this way:
And here's a link to the Debuggex Demo for those who want to play around with it in more detail.
What am I doing wrong?
You can just match everything that doesn't have a double underscore like this.
/^((?!__).)*$/gm
And it turns out that the issue was simply that I was using the wrong syntax for negative look-aheads. (?!=__) is wrong, (?!__) is right.
The corrected regular expression:
^./((?!__)[^/]+?(?!__)/)*((?!__)[^/]+?(?!__)).jsx?$