assistance with regex and multiple conditions - javascript

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

Related

How to convert this in a JavaScript Code

I'm very new on this and I isn't a pro on this issues!
I'm just here for request for your help because I know this community is the best for this problems!
Actually I'm using a software called "Bulk Rename Utility", it works for bulk rename files with a very useful interface, one of them is the RegEx option where you can rename files thanks to the regular expresion lenguage.
I was wonder if I can convert this code in a JavaScript code, this is because I need to modify various parameters and it could be better if I can use a JavaScript code.
As I told you, I'm very new on this. After checking some videos I learned how to use the RegEx option in this program and the codes I got was:
In the "Match" field of the program I write: (.*) 10-1[A-Z] (.*).
In the "Replace" filed of the program I write: \1_REF_\2
A then, it do the magic!
As I said, I want to know how I can convert this code to a JavaScript code because I have a lot of this codes and I want just one whole code to make a bulk process.
Thanks you so much!
Like this?
const str = 'abc 10-1G def';
const outputStr = str.replace(/(.*) 10-1[A-Z] (.*)/, '$1REF$2');
console.log(outputStr);
You just need to put $1/$2 in place of the captured groups you want in the replaced string.
To insert code, you can put the code between backticks (`) and you'll be able to type *s normally.

Javascript regex to match exact folder path

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])?\/?

Find all `*.html` but not `*.tmp.html` using JavaScript regex

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

Improve Regex to match font files in css files

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\("(.*?)"\)/

Match paths that do not have __magic__ folders

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?$

Categories