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
Related
I am trying to see if a string is a valid file name and path. Right now I am using regex to do it but seems after typing a longer length of string it uses lots of CPU and makes the browser to be un-progressive.
public static readonly INVALID_FILE_NAME_REGEX: RegExp = /([a-zA-Z0-9 _#\-^!#$%&+={}./\\\[\]]+)+\.[a-zA-Z0-9]+$/;
and I use test to check it
INVALID_FILE_NAME_REGEX.test(myFilePath);
I was wondering if there is any way to check the file name and path is correct without Regex, or something does not use lots of resources?
here is example of valid and invalid paths.
invalid
path/
/path
/path/folder
/path/
valid
file.txt
/path/folder/file.txt
file.TXT
Thanks
Try this one:
/^[a-zA-Z0-9 _#\-^!#$%&+={}./\\\[\]]+\.[a-zA-Z0-9]+$/
Try wiht this, it worked for me.
^([\/][a-zA-Z0-9]+)*[\/]?[a-zA-Z0-9]+[.][a-zA-Z0-9]+$
Example 1:
https://rubular.com/r/S7QydSU6jnuCLd
Example 2:
https://rubular.com/r/sYARkEfsDJO15f
This page helps you to check you RegExp
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/
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 script that pulls in the file from the server and places it in a folder.
However all I need is the fist part of the file name so I was wondering is there is a way of deleting the rest of the file name from a certain point.
For example 5018228_Prince_+_L$_Mn03_Mx98_Tr00_Tc300__39L_F.psd is the file name, I don't need the part from the dollar sign $ onward so it would look like this 5018228_Prince_+_L.psd
Solution using Regex
You can use regex with capturing groups and get the expected result.
var fileName ="5018228_Prince_+_L$_Mn03_Mx98_Tr00_Tc300__39L_F.psd";
var reqName = fileName.replace(/(.*)\$.*(\.[a-z]*)/,"$1$2");
console.log(reqName);
So in the above regex I use 2 capturing groups using ( ) which will be stored in $1 and $2 later during replace. So while replacing I use only the captured groups hence omitting the non required (non captured) part of string.
The captured sub-string are as represented below.
(5018228_Prince_+_L)$_Mn03_Mx98_Tr00_Tc300__39L_F(.psd)
//------------------ -------
//-------^^ this is $1-----------------------------^^ this is $2------------
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!