Replace file path in Node JS - javascript

I am having a ridiculous time trying to simply remove a path from a pathname in Node JS. I think the problem is that replace is not working because the base string has slashes. But I can't seem to figure out any way to operate on the string properly. When I do replace sometimes it just removes the slashes entirely and doesn't even replace with what I asked it to.
Example.... where the heck did the slashes even go.
'C:\path\build\test\subfolder'.replace('b', 'z')
// "C:path\build\testsuzfolder"
Anyway what I'm actually trying to do is this.
Given this path I get.
C:\path\build\test\subfolder
Remove
C:\path\build\test\
But no amount of attempts with replace is working, even if I escape slashes.

Node was giving me __dirname with those slashes so there was no string function to do what I needed. So I had to use split with Node's special path.sep and then rejoin with the other slash type.
let formatted_folder = folder.split(path.sep).join('/');

According to your description of what you want, you should do
const path = require('path');
const idx = __dirname.lastIndexOf(path.sep);
const res = __dirname.slice(idx);
(or idx + 1, if you don't need the separator itself in the result).

Related

RegEx for detecting a string and a path in one go

Here is an example of what regex I need regex
I have many of these lines in a file
build test/testfoo/CMakeFiles/testfoo2.dir/testfoo2.cpp.o: CXX_COMPILER__testfoo2_Debug /home/juxeii/projects/gtest-cmake-example/test/testfoo/testfoo2.cpp || cmake_object_order_depends_target_testfoo2
I need to detect the string between CXX_COMPILER__ and _Debug, which here is testfoo2.
At the same time, I need to also detect the entire file path /home/juxeii/projects/gtest-cmake-example/test/testfoo/testfoo2.cpp, which comes always after the first match.
I could not figure out a regex for this. So far I have .*CXX_COMPILER__(.\w+)_\w+|(\/[a-zA-Z_0-9-]+)+\.\w+ and I am using it in typescript like so:
const fileAndTargetRegExp = new RegExp('.*CXX_COMPILER__(.\w+)_\w+|(\/[a-zA-Z_0-9-]+)+\.\w+', 'gm');
let match;
while (match = fileAndTargetRegExp.exec(fileContents)) {
//do something
}
But I get no matches. Is there an easy way to do this?
Will it always have the || <stuff here> at the end? If so, this regex based on the one you provided should work:
/.*CXX_COMPILER__(\w+)_.+?((?:\/.+)+) \|\|.*/g
As the regex101 breakdown shows, the first capturing group should contain the string between CXX_COMPILER__ and _Debug, while the second should contain the path, using the space and pipes to detect where the latter ends.
let line = 'build test/testfoo/CMakeFiles/testfoo2.dir/testfoo2.cpp.o: CXX_COMPILER__testfoo2_Debug /home/juxeii/projects/gtest-cmake-example/test/testfoo/testfoo2.cpp || cmake_object_order_depends_target_testfoo2';
const matches = line.match(/.*CXX_COMPILER__(\w+)_.+?((?:\/.+)+) \|\|.*/).slice(1); //slice(1) just to not include the first complete match returned by match!
for (let match of matches) {
console.log(match);
}
If the pipes won't always be there, then this version should work instead (regex101):
.*CXX_COMPILER__(\w+)_.+?((?:\/(?:\w|\.|-)+)+).*
But it requires you to add all of the valid path characters individually every time you realize a new one might be there, and you'll need to make sure the paths don't have spaces because adding space to the regex would make it detect the stuff after the path too.
Looks good, but you need delimiters. Add "/" before and after your Regex - no quotation marks.
let fileContents = 'build test/testfoo/CMakeFiles/testfoo2.dir/testfoo2.cpp.o: CXX_COMPILER__testfoo2_Debug /home/juxeii/projects/gtest-cmake-example/test/testfoo/testfoo2.cpp || cmake_object_order_depends_target_testfoo2';
const fileAndTargetRegExp = new RegExp(/.*CXX_COMPILER__(.\w+)_\w+|(\/[a-zA-Z_0-9-]+)+\.\w+/, 'gm');
let match;
while (match = fileAndTargetRegExp.exec(fileContents)) {
console.log(match);
}
Here's my way of doing it with replace:
I need to detect the string between CXX_COMPILER__ and _Debug, which is here testfoo2.
Try to replace all characters of the string with just the first captured group $1 which is between CXX_COMPILER__ and _Debug:
/.*CXX_COMPILER__(\w+)_Debug.*/
^^^^<--testfoo2
I need to also detect the entire file path /home/juxeii/projects/gtest-cmake-example/test/testfoo/testfoo2.cpp
The same, just this time replace all just leave the second matched group which is anything comes after our first captured group:
/.*CXX_COMPILER__(\w+)_Debug\s+(.*?)(?=\\|\|).*/
^^^<-- /home/.../testfoo2.cpp
let line = 'build test/testfoo/CMakeFiles/testfoo2.dir/testfoo2.cpp.o: CXX_COMPILER__testfoo2_Debug /home/juxeii/projects/gtest-cmake-example/test/testfoo/testfoo2.cpp || cmake_object_order_depends_target_testfoo2'
console.log(line.replace(/.*CXX_COMPILER__(\w+)_Debug.*/gm,'$1'))
console.log(line.replace(/.*CXX_COMPILER__(\w+)_Debug\s+(.*?)(?=\\|\|).*/gm,'$2'))

Removing elements of string before a specific repeated character in it in javascript

I'm trying to remove from my string all elements before an specific character which is repeated several times in this way:
let string = http://localhost:5000/contact-support
thus I´m just trying to remove everything before the third /
having as result:contact_support
for that i just set:
string.substring(string.indexOf('/') + 3);
Bust guess thats not the correct way
Any help about how to improve this in the simplest way please?
Thanks in advance!!!
It seems like you want to do some URL parsing here. JS brings the handful URL utility which can help you with this, and other similar tasks.
const myString = 'http://localhost:5000/contact-support';
const pathname = new URL(myString).pathname;
console.log(pathname); // outputs: /contact-support
// then you can also remove the first "/" character with `substring`
const whatIActuallyNeed = pathname.substring(1, pathname.length);
console.log(whatIActuallyNeed); // outputs: contact-support
Hope This will work
string.split("/")[3]
It will return the sub-string after the 3rd forward slash.
You could also use lastIndexOf('/'), like this:
string.substring(string.lastIndexOf('/') + 1);
Another possibility is regular expressions:
string.match(/[^\/]*\/\/[^\/]*\/(.*)/)[1];
Note that you must escape the slash, since it is the delimiter in regular expressions.
string.substring(string.lastIndexOf('/')+1) will also do the job if you are looking to use indexOf function explicitly.

Regex for filename at end of href attribute (replace filepath with filename)

I am trying to replace the filepath with just the filename using regex, and struggling.
I take a list of text like this:
xlink:href="file://C:\x\y & DRAWINGS\z - CONTROLLED\a \testsvg-A01.svg"
and i just want to output
xlink:href="testsvg-A01.svg"
I can get there with separate regex (javascript) with several bits of regex as such:
let inQuotes = hrefs[0].match(/"(.*?)"/gm);
inQuotes = inQuotes[0].match(/([^\\]+$)/gm);
inQuotes = inQuotes[0].replace(/"/g, "");
this will return just the filename, but i was wondering if there was a way to take this and replace the original text to the desired style.
EDIT:
i can get it for a single occurrance with this line of code:
let testHrefs = outText.match(/xlink:href="(.*?)"/gm)[0].match(/"(.*?)"/gm)[0].match(/([^\\]+$)/gm)[0].replace(/^/, 'xlink:href="');
but it looks awful and doesn't completely do what i want. Any advice?
You could use a regex to remove the text you don't want to keep (i.e. everything between the href=" and the filename:
let href = 'xlink:href="file://C:\\x\\y & DRAWINGS\\z - CONTROLLED\\a \\testsvg-A01.svg"';
console.log(href);
console.log(href.replace(/href=".*?([^\\\/]+)$/, 'href="$1'));
Note I've used [\\\/] to allow for Unix and Windows style filenames, if you only want Windows style filenames that can simply be [\\].
First, keep in mind that backslashes in strings need to be escaped, as the backslash itself is an escape character. You'll likely want to escape these as \\.
After doing this, you can make use of a positive look-behind on the \ with the regex (?<=\\)[^\\]*:
var xlink = "file://C:\\x\\y & DRAWINGS\\z - CONTROLLED\\a \\testsvg-A01.svg";
var regex = /(?<=\\)[^\\]*/gm;
console.log(xlink.match(regex));
This splits the string into each of the folders (which may be useful within itself), though if you exclusively want the filename, you can use xlink.match(regex)[4] (assuming the length is consistent), or xlink.match(regex)[xlink.match(regex).length - 1] if it isn't.
var xlink = "file://C:\\x\\y & DRAWINGS\\z - CONTROLLED\\a \\testsvg-A01.svg";
var regex = /(?<=\\)[^\\]*/gm;
console.log(xlink.match(regex)[xlink.match(regex).length - 1]);

Get base url from string with Regex and Javascript

I'm trying to get the base url from a string (So no window.location).
It needs to remove the trailing slash
It needs to be regex (No New URL)
It need to work with query parameters and anchor links
In other words all the following should return https://apple.com or https://www.apple.com for the last one.
https://apple.com?query=true&slash=false
https://apple.com#anchor=true&slash=false
http://www.apple.com/#anchor=true&slash=true&whatever=foo
These are just examples, urls can have different subdomains like https://shop.apple.co.uk/?query=foo should return https://shop.apple.co.uk - It could be any url like: https://foo.bar
The closer I got is with:
const baseUrl = url.replace(/^((\w+:)?\/\/[^\/]+\/?).*$/,'$1').replace(/\/$/, ""); // Base Path & Trailing slash
But this doesn't work with anchor links and queries which start right after the url without the / before
Any idea how I can get it to work on all cases?
You could add # and ? to your negated character class. You don't need .* because that will match until the end of the string.
For your example data, you could match:
^https?:\/\/[^#?\/]+
Regex demo
strings = [
"https://apple.com?query=true&slash=false",
"https://apple.com#anchor=true&slash=false",
"http://www.apple.com/#anchor=true&slash=true&whatever=foo",
"https://foo.bar/?q=true"
];
strings.forEach(s => {
console.log(s.match(/^https?:\/\/[^#?\/]+/)[0]);
})
You could use Web API's built-in URL for this. URL will also provide you with other parsed properties that are easy to get to, like the query string params, the protocol, etc.
Regex is a painful way to do something that the browser makes otherwise very simple.
I know that you asked about using regex, but in the event that you (or someone coming here in the future) really just cares about getting the information out and isn't committed to using regex, maybe this answer will help.
let one = "https://apple.com?query=true&slash=false"
let two = "https://apple.com#anchor=true&slash=false"
let three = "http://www.apple.com/#anchor=true&slash=true&whatever=foo"
let urlOne = new URL(one)
console.log(urlOne.origin)
let urlTwo = new URL(two)
console.log(urlTwo.origin)
let urlThree = new URL(three)
console.log(urlThree.origin)
const baseUrl = url.replace(/(.*:\/\/.*)[\?\/#].*/, '$1');
This will get you everything up to the .com part. You will have to append .com once you pull out the first part of the url.
^http.*?(?=\.com)
Or maybe you could do:
myUrl.Replace(/(#|\?|\/#).*$/, "")
To remove everything after the host name.

`String.raw` when last character is `\`

String.raw is very useful. For example:
let path = String.raw`C:\path\to\file.html`
However, when the last character of the template string is \, it becomes an syntax error.
let path = String.raw`C:\path\to\directory\`
Uncaught SyntaxError: Unterminated template literal
Tentatively, I use this method.
let path = String.raw`C:\path\to\directory\ `.trimRight()
Can I write template string which last character is \ using String.raw?
Here are a few different possible workarounds. My favorite is probably creating a custom template tag dir to solve the problem for you. I think it makes the syntax cleaner and more semantic.
let path
// Uglier workarounds
path = String.raw `C:\path\to\directory${`\\`}`
path = String.raw `C:\path\to\directory`+`\\`
path = `C:\\path\\to\\directory\\`
// Cleanest solution
const dir = ({raw}) => raw + `\\`
path = dir `C:\path\to\directory`
console.log(path)
According to https://hacks.mozilla.org/2015/05/es6-in-depth-template-strings-2/ escape sequences are left untouched, aside from \$ \{ and \`, in template strings. Thus one cannot escape the escape character. Based on this it looks like your solution is the best way to achieve your goal.

Categories

Resources