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?$
Related
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
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])?\/?
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'm trying to match font files that are contained in CSS files.
The font files can have the following extensions :
And following format :
- url("../../../../fonts/font_awesome/fontawesome-webfont.ttf?v=4.6.3")
- url("../../../../fonts/font_awesome/fontawesome-webfont.svg?v=4.6.3#fontawesomeregular")
In this case the match should be :
../../../../fonts/font_awesome/fontawesome-webfont.ttf?v=4.6.3
../../../../fonts/font_awesome/fontawesome-webfont.svg?v=4.6.3#fontawesomeregular
I have already made a regex that matches them successfully, however on using an online service, the regex returned to me a warning saying that it has a catastrophic backtracking, so I'm asking how can this Regex be better :
url\s*\(\s*\S(.+?\.(woff|eot|woff2|ttf|svg)\s*)\??\S+?\s?\)
Regex in action with the backtracking warning : https://regex101.com/r/KWsh1X/1
This will do the job:
url\("([^\)]+?\.(woff|eot|woff2|ttf|svg)[^"]*)"
Here you can play around: https://regex101.com/r/KWsh1X/2
Look like it's late but maybe will be helpfull too
url\(['"]([^"']+(woff|eot|woff2|ttf|svg)[^"']+)
\W+url\W+"([^"]+\.(woff|eot|woff2|ttf|svg)[^"]+)"\W+
You don't have to do as many steps if you don't include the file formats.
/url\("(.*?)"\)/
I am using the YUI3 library and am using a filter to match and replace parts of a URL.
Because filter is not very flexible, I am only able to provide a regex expression for searching and then a string for replacing the matches:
filter: {
searchExp : "-min\\.js",
replaceStr: "-debug.js"
}
In my case, I have a URL that looks like this:
http://site.com/assets/js?yui-3.9.0/widget-base/assets/skins/sam/widget-base.css&yui-3.9.0/cssbutton/cssbutton-min.css
I would like to match /assets/js if there are .css files. If the parameters contain a CSS file, then it will always only contain CSS files.
So far, I have written a small regex to check for the presence of .css at the very end:
.*\.css$
However, now, if we have a match, I would like to return /assets/js as the match. Is this something that is doable with regex?
Personally, I would rather this be done with a simple function and a simple if/else, but due to the limitations (I can only use regex), I need to find a regex solution to this.
This is a bit hacked together, but should do the job:
var t = new RegExp( "/assets/js(([^\\.]*\\.)*[^\\.]*\\.css)$" )
document.write( "http://site.com/assets/js?yui-3.9.0/widget-base/assets/skins/sam/widget-base.css&yui-3.9.0/cssbutton/cssbutton-min.css".replace( t, "/newthing/$1" ) );
Essentially it searches for /assets/js, followed by any characters, followed by .css. If the whole thing matches it wil replace it with the new text, and include the matched pattern (from the first brackets) after it. Everything from before /assets isn't included in the match, so doesn't need to be included.
I imagine your library uses replace internally, so those strings should work. Specifically,
"/assets/js(([^\\.]*\\.)*[^\\.]*\\.css)$"
"/newthing/$1"
I'm not quite sure what you want to do with the results, but this allows you to change the folder and add suffixes (as well as check for the presence of both tokens in the first place). To add a suffix change the replacement to this:
"/assets/js$1-mysuffix"