regex match extension but exclude a specific file - javascript

I am in webpack.config where I need to include extension to match specific file type, but I need to exclude a specific file. I googled it but didn't find a perfect solution.
What I have : /\.(?:css|less)$/ -> matches all files that have .less OR .css but here I want to add "not only -> test.something_strict.less" /\.(?:css|less|!test.less)$/ where test.something_strict is a file name with extension .less and only exclude test.something_strict
but this didn't work, this matches .less or .css and pass through the test.

|!test.less doesn't really negate matching test.less. It will literally match ! before test.less.
You can use this regex:
/^(?!test\.[^.]+\.less$).+\.(?:css|less)$/m
RegEx Demo
(?!test\.less$) is a negative lookahead that will assert failure if filename is test.<anything>.less.

Related

jQuery replace a subdirectory with prefix and suffix

I want to replace a sub directory that has the prefix /s72 and the suffix /. Example:
https://www.example.com/dsasdsad/iufnasdadaso/s72/first-picture.img
https://www.example.com/efggvdfb/pothgpbmfkoe/s72-c/second-picture.img
https://www.example.com/jyhjgfdf/rokomvcrvkmw/s72-w222-h888/third-picture.img
https://www.example.com/pokmhfds/qprigmvmspej/s72-c-d/fourth-picture.img
I tried to change /s72/, /s72-c/, /s72-w222-h888/, /s72-c-d/, and other /s72 subdirectory with /w100-h100-c/ using jQuery .replace(). If I can't use jQuery .replace(), can I replace it in another way?
I have tried:
.replace(/\/s72\S+/g, "/w100-h100-c/")
But the result is:
https://www.example.com/dsasdsad/iufnasdadaso/w100-h100-c/
https://www.example.com/efggvdfb/pothgpbmfkoe/w100-h100-c/
https://www.example.com/jyhjgfdf/rokomvcrvkmw/w100-h100-c/
https://www.example.com/pokmhfds/qprigmvmspej/w100-h100-c/
The filename is lost.
You were trying to match all non-whitespace characters until the end
try this
.replace(/\/s72\S*\//g, "/w100-h100-c/")
https://regex101.com/r/UxZDSa/1

there is no char between 2 group with regex

I need a regexp to filter a list of paths.
my sample list is below:
models/user.js
models/adapter.js
models/acquire.js
models/schema/extension.js
models/schema/permission.js
modules/breaktime/models/break.js
modules/breaktime/models/rule.js
modules/breaktime/models/step.js
modules/pbxmanager/models/group.js
modules/pbxmanager/models/member.js
modules/pbxmanager/models/shift.js
modules/breaktime/models/request.js
modules/breaktime/models/state.js
I'd define the exact start path and want to get only the files under that path, not from subfolders.
For example; if I set models/ as a starter string in the regexp, I should only get first 3 line, not those under schema folder.
I tried to make groups like (^start string)(exactly nothing)(end string$) but no luck.
(^models\/)[\w{0}](\/[\w]+\.js$)
https://regex101.com/r/wNtCni/1
I couldn't find how to set "nothing between two group" in regexp.
If you use a character class [\w{0}] there is at least a single char expected, either a word character, {, 0, or }
In your case you don't want that and you can omit it the character class which will give you (^models\/)(\/[\w]+\.js$) which has a forward slash too much.
If you remove that extra / as well, and remove the unnecessary groups and brackets, you will get
^models\/\w+\.js$
Regex demo

Regex - Test extension if pass then test file name

Is it possible in regex to check file name if it pass the extension test,
i have a regex that matches .local OR .txt for the file like something.local OR something.txt. Is there anyway to strict one file name in the test ?
Right now i am using /\.(?:local|txt)$/ but don't know how to implement that condition.
for example file name is test.raw.local so i want to check if it has raw.local then ignore that file .
I try /\.(?:local|txt|!raw.local)$/ but didn't work!!
valid:
-> test.local, hello.txt, script.something.local
invalid:
-> test.raw.local
You can use a negative lookaheads in addition to your assertion for strings you want:
(?!.*\.raw\.local$)^.+\.(?:local|txt)$
Regex101 Example

Using ignorePlugin to ignore one folder from dependencies.

I've realized that one of a big folder inside one of my dependencies is not required. But since it takes almost 30kb of my build size, I wish to ignore it from the final webpack build. For that, i'm trying to use the webpack ignorePlugin.
This is what i've tried till now:
new webpack.IgnorePlugin(/^\.\/precomputed$/, /elliptic$/),
The absolute path for the folder that I wanna ignore is :
node_modules/elliptic/precomputed
What i'm doing wrong here ? Is there anything else required ? Thanks for help ! Appreciate it :)
The regular expressions you're using don't match the path of the module.
/^\.\/precomputed$/ matches only exactly ./precomputed. Because it's using the beginning of string ^ and the end of the string $, and in between is only ./precomputed.
/elliptic$/ matches every string that ends in elliitic.
The correct regular expression for your module would be:
/elliptic\/precomputed$/
But this matches only the modules that end in elliptic/precomputed (because of the $ anchor). For instance elliptic/precomputed.js or elliptic/precomputed/submodule are not matched. If you intend to include these as well you can remove the $ and it will match all of them:
/elliptic\/precomputed/
Try it in Regexr, when you add $ to the pattern you'll see that only the first one matches.

Trying to find the regex for pretty URLs

I have a website structure like this:
/docs/one_of_the_resources/one-of-the-resources.html
/docs/a_complete_different_resource/a-complete-different-resource.html
I want to get rid of all sub-folders in the url and get this:
/one-of-the-resources.html
/a-complete-different-resource.html
Sub-folders should not be affected:
/docs/one_of_the_resources/assets/*
The folder name is always the same as the html file just dashes are swapped with underline and of course there is no suffix.
I'm using grunt-contrib-rewrite and grunt-connect.
Can't wrap my head around it. Is this even possible?
You can use a negated character class
/\/[^/]+$/
[^/]+ Matches anything other than a /. The quantifier + ensures one or more characters.
$ Anchors the regex at the end of the string.
Regex Demo
Example
string = "/docs/one_of_the_resources/one-of-the-resources.html";
console.log(string.match(/\/[^/]+$/)[0]);
// => one-of-the-resources.html

Categories

Resources