0

I am trying to remove a string from text.txt. text.txt file contains following format of string

text/more/more.txt
text/home.txt
text/more/yoo/yoo.txt
text/about.txt

Now what I am doing is watching a folder and when any of the above listed file, lets say text/about.txt, is deleted then text.txt file should automatically be updated to following

text/more/more.txt
text/home.txt
text/more/yoo/yoo.txt

For this I am using hound module to keep watching for delete event. And replace module to replace deleted path from text.txt file. Below is my code

watcher.on('delete', function(file, stats) {

    replace({
        regex: /file/g, // file is something like this text/about.txt
        replacement: '',
        paths: [path + '/text.txt'],
        recursive: true,
        silent: true,
    });

});

But my above code does not remove particular string i.e. file from text.txt file. How can I solve this?

UPDATE

file in above code has this value text/about.txt.

2
  • Just a thought, but maybe take a look at the file variable. It could have special characters that need to be escaped before passing it in to your regex. Commented Nov 27, 2014 at 18:22
  • The only special character it has is \n. I am not sure if you are refering this as a special character or not. Commented Nov 27, 2014 at 19:12

2 Answers 2

1

This is a error in semantics, you misinterpreted what happens when you do this:

watcher.on('delete', function(file, stats) {
    ...
    regex: /file/g, // file is something like this text/about.txt
    ...
}

Here, file in the RegExp object is looking for a string called file, not the actual variable contents of the String object you're passing into the function. Do this instead:

    regex: new RegExp(file, 'g'), // file is something like this text/about.txt

See RegExp for more details.

Sign up to request clarification or add additional context in comments.

2 Comments

great it works now. But it does remove that text/about.txt but it also leaves the new line. How can I remove the new line including text/about.txt?
Like this: regex: new RegExp(file + '\\n', 'g'),.
1

I have updated variable search_content and replace_content to handle special characters also and then using fs module to replace all strings in a file. Also you can run a synchronous loop on a files to replace strings using callbacks.

// Require fs module here.
var search_content = "file";
var replace_content = '';
var source_file_path = '<<source file path where string needs to be replaced>>';
search_content = search_content.replace(/([.?&;*+^$[\]\\(){}|-])/g, "\\$1");//improve
search_content = new RegExp(search_content, "g");
fs.readFile(source_file_path, 'utf8', function (rfErr, rfData) {
    if (rfErr) {
        // show error
    }
    var fileData = rfData.toString();
    fileData = fileData.replace(search_content, replace_content);
    fs.writeFile(source_file_path, fileData, 'utf8', function (wfErr) {
        if (wfErr) {
            // show error
        }
        // callback goes from here
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.