How to extract directory from a path by a glob pattern? - javascript

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/

Related

Regular expression for folder path validation in javscript

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

Is there something like glob but for URLs, in JavaScript?

I need to match URLs against string patterns but I want to avoid RegExp to keep the patterns simple and readable.
I'd like to be able to have patterns like http://*.example.org/*, which should be equivalent of /^http:\/\/.*\.example.org\/.*$/ in RegExp. That RegExp should also illustrate why I want to keep it more readable.
Basically I'd like glob-like patterns that work for URLs. The Problem is: normal glob implementations treat / as a delimiter. That means, http://foo.example.org/bar/bla wouldn't match my simple pattern.
So, an implementation of glob that can ignore slashes would be great. Is there such a thing or something similar?
You can start with a function like this for glob like behavior:
function glob(pattern, input) {
var re = new RegExp(pattern.replace(/([.?+^$[\]\\(){}|\/-])/g, "\\$1").replace(/\*/g, '.*'));
return re.test(input);
}
Then call it as:
glob('http://*.example.org/*', 'http://foo.example.org/bar/bla');
true
Solved the problem by writing a lib for it:
https://github.com/lnwdr/calmcard
This matches arbitrary strings with simple wildcards.

Regex if substring exist then match another part of the string

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"

JavaScript regular expression for matching URL path components

What JavaScript regular expression should I use to match individual components of a URL path? By path, I mean the path of the resource on the server, e.g. if the URL is 'http://example.com/directory/resource?start=0', the path is '/directory/resource'. By path components, I mean the /-separated parts of the path.
Let's say we have the URL 'http://example.com/component1/component2'. What I would like is to be able to match 'component1' or 'component2' with a grouped regular expression for each, so each component can be extracted, i.e. something like this: 'http://example.com/($component-regex)/($component-regex) ($component-regex being the regular expression we need to devise). In this example, there would be two matched groups: 'component1' and 'component2'.
Please come up with a regex that's considered safe by JSLint :) For example, it considers [^/]+ insecure.
You don't need a regex for this:
var components = url.split(/[?#]/)[0].split("/").slice(3);
Okay, you do need a regex to split on one of two possible characters, but you could do it without any regex with this:
var components = url.split("#")[0].split("?")[0].split("/").slice(3);

Using Regex to match the middle of a path?

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!

Categories