jQuery replace a subdirectory with prefix and suffix - javascript

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

Related

Match URL and it's sub directories

How would I write a regex to match /path/subpath and all /path/subpath/*, but not /path/subpathrandomcharacters
I am currently trying this \/path\/subpath.*
But this is also matching /path/subpathrandomcharacters
After you check the value of the path/subparth, then it can either have a slash then anything or nothing.
\/path\/subpath(\/.*)*
https://www.regextester.com/?fam=103013

Regex CSS full path

I would like to regex the fullpath of the css, for example I have this:
<link rel="stylesheet" href="../assets/myCssFile.css"/>
I would like to regex: /assets/myCssFile.css
What I tried is this: /(?:href)=("|').*?([\w.]+\.(?:css))\1/gi
but this returns me this: href="../assets/myCssFile.css"
Can someone help me out with the regex.
BTW: It is an response text of an ajax request which returns me a string of the html page
href=\"(.*\.css)"
This will return "../assets/myCssFile.css" in a capture group.
https://regex101.com/r/a44Axz/1
All it is doing is saying:
I am only interested in text within quotes that immediately follows an "href" and only if it is a ".css" file.
Late, but maybe it will help someone. :)
Another regex that matches css paths:
href[ \t]{0,}=[ \t]{0,}"(.{1,}\.css)"
This also matches cases when between href and = exists tabs or spaces or between = and css filename.
Also, if you want to match css and javascript filenames in one regex you can use something like this:
href[ \t]{0,}=[ \t]{0,}"(.{1,}\.css)"|src[ \t]{0,}=[ \t]{0,}"(.{1,}\.js)"
They are combined by or operator |.

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

Regexp - from first character to & character

I have string like this: http://someurl.com?test&lettersg and I would like to mach part from first letter to & (without &, this part only: http://someurl.com?test).
var match = /^[^&]*/.exec("http://someurl.com?test&lettersg")[0];
If its the code you want, look at the source of this JQuery Plugin (URL Parser). Or you can just go ahead and use the plugin.
For URL manipulation I suggest using URI.js.
Your problem can be solved with and without RegExp:
var string = "http://someurl.com?test&lettersg";
console.log(string.match(/^[^&]+/)[0]);
console.log(string.substring(0, string.indexOf('&')));

Categories

Resources