Replacing string contains forward slashes in __filename in NodeJs

Replacing string contains forward slashes in __filename in NodeJs



I'm using __filename variable in NodeJS ES7 and would like to replace file path


__filename



from


c:/web/google-web/tests/selenium/tests/desktop/main/login.js



to


c:/web/google-web/Results/desktop/main/login.log



I tried this code:


console.log(__filename.replace("tests/selenium/tests", "Results").replace('.js','.log'));

console.log(__filename.replace("tests/selenium/tests", "Results").replace('.js','.log'));

console.log(__filename.replace(///g, "-").replace("tests-selenium-tests", "Results").replace('.js','.log'));



I tried How to globally replace a forward slash in a JavaScript string? too but no luck.






Hi Jitesh. I am a volunteer editor, and I note that your questions are a bit too chatty. We prefer technical writing here, and since readers know you need help, the addition of "please help me" and similar is likely to be interpreted as begging and pleading. I recommend reading this discussion to understand this in greater clarity: How can we encourage (new) authors to ask confident questions?. I have edited your questions before: please view all edits to your questions, so you can become increasingly aware of our shared writing standards.

– halfer
Sep 7 '18 at 8:08







I got an answer after spending a day to due to dealing of special character like slash not when i asked the question. I hope you understand this and stack overview allows to post an answer if we get it after spending lot of efforts, i think it might have taken as different way like i was knowing the answer.

– Jitesh Sojitra
Sep 23 '18 at 18:45






It is false alarm who-so-ever deleted. Any one person/administrator can't delete the answer according to its personal opinion/judgement. I disagree with all these happening in this question, but i didn't say anything. I started this discussion after your questions and hurting individual's question/answer which i had provided genuinely.

– Jitesh Sojitra
Sep 24 '18 at 7:03






I don't understand your opinion about "hurting individual's question/answer", but if your deleted answer below was really the solution, then I suggest: (1) edit it to provide more detail, since it is rather sparse presently, then (2) flag for a moderator to ask for it to be undeleted. [I have deleted some of my comments in this thread to focus on the remaining issue].

– halfer
Sep 24 '18 at 9:16





1 Answer
1



If what you've tried isn't working I can only assume that __filename isn't what you expect it to be.


__filename



Your first two examples work just fine (the third doesn't switch hyphons back to slashes), and you can verify this by running:


"c:/web/google-web/tests/selenium/tests/desktop/main/login.js".replace("tests/selenium/tests", "Results").replace('.js','.log') === "c:/web/google-web/Results/desktop/main/lfghogin.log"; // true



Since your path starts c:/ I'm guessing you're on Windows? If so, you've got the slashes the wrong way round in your replacement.


c:/



If you want something generic to handle that then use .replace(/tests[\/]selenium[\/]tests/,'Results')


.replace(/tests[\/]selenium[\/]tests/,'Results')



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế