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.
Related
In the source code directory, I want to move a file into a subfolder in current directory
example:
from
root/index.js
into
root/subfolder/index.js
In index.js I have many import lines like this:
import Styles from '../../../Styles';
The point is, I want to replace '../../../Styles'; with '../../../../Styles';
Can anyone help me with that? I tried to replace (\.\.\/)+ with ../$1 but It only returns ../../
replace with regex in Visual Studio Code
I need to validate the folder path in javascript, which should contain the workspace(any string is acceptable) as a folder
"/homes/nb/workspace3/sdddsdd".match(/^\/(.*)\/workspace(.*)$/g)
Below should pass when workspace with any integer or characters is present
/homes/suresh/workspace3
/homes/nb/workspace
/volume/kiran/workspace123
/homes/nb/workspace3233
/homes/nb/workspace123
/homes/nb/workspace012
/volume/kiran/workspace
below case, it should fail that means it should not contain a further folder name after a workspace
/volume/kiran/workspace123/hbdhdhjs
/volume/kiran/workspace/hbdhdhjs
/homes/nb/workspace012/dsdsd
The last part should match
/workspace[^\/]*$/
see https://regex101.com/r/Ti950e/1
Note that you don't need to match before workspace because
it is simply the target string.
If you want to allow a trailing / only at the end, use this instead
/workspace[^\/]*\/?$/
see https://regex101.com/r/veXzXm/1
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 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 var showNo that's an input for the beginning of a directory.
example: var showNo = "101B"
After showNo are characters that include spaces and other junk set up on the network set by another department.
example: /101B A Trip to the Beach/
I need to use sub directories inside of this one:
example: /101B A Trip to the Beach/assets/tools/
Is there a way to use regex and the variable to avoid scanning all of the directories and trying to match a substring of the first 4 characters?
var directory = str.match(/\/101B[^\/]+\//)[0];
Will match to the first directory name that starts with you variable.
More importantly the idea is as follows :
Match the first four character string literal that starts with a directory slash.
Then match any character that is not a directory slash. The "is not" is indicated by the ^.
Then repeat 2 an additional 0 or more times.
Finally match the directory slash.
I suspect you had trouble with the "anything that is NOT" character class. It is sometimes tricky but once you get it it is a very useful short cut.
--edit--
Actually on re reading I suspect you had trouble with using the variable inside the regex, correct?
That's easy enough, too, once you know how.
You can construct it as a string first:
var regex_string = "/" + showNo + "[^/]+/";
And then "compile" it into a regex which you can use as normally :
var regex_dir = RegExp(regex_string);
var directory = str.match(regex_dir);
Hope this helps!