javascript - Replace multiple strings in a file using nodejs - Stack Overflow

I have a json file in which I want to replace two words with two new words using fs of node js. I read

I have a json file in which I want to replace two words with two new words using fs of node js. I read the solution to a similar query on Replace a string in a file using nodejs. Then I tried the following code snippet but it replaces only a single word. I need to run it multiple times to replace multiple words.

Here is the code snippet:

var fs = require('fs')
fs.readFile('C:\\Data\\sr4_Intellij\\newman\\Collections\\api_collection.json', 'utf8', function (err,data) {
  if (err) {
    return console.log(err);
  }
  var result1 = data.replace(/{{Name}}/g, 'abc');
  var result2 = data.replace(/{{Address}}/g, 'xyz');
  var arr1 = [result1,result2]
  console.log(arr1[0]);

  fs.writeFile('C:\\Data\\sr4_Intellij\\newman\\Collections\\api_collection.json', arr1, 'utf8', function (err) {
     if (err) return console.log(err);
  });

});

I have a json file in which I want to replace two words with two new words using fs of node js. I read the solution to a similar query on Replace a string in a file using nodejs. Then I tried the following code snippet but it replaces only a single word. I need to run it multiple times to replace multiple words.

Here is the code snippet:

var fs = require('fs')
fs.readFile('C:\\Data\\sr4_Intellij\\newman\\Collections\\api_collection.json', 'utf8', function (err,data) {
  if (err) {
    return console.log(err);
  }
  var result1 = data.replace(/{{Name}}/g, 'abc');
  var result2 = data.replace(/{{Address}}/g, 'xyz');
  var arr1 = [result1,result2]
  console.log(arr1[0]);

  fs.writeFile('C:\\Data\\sr4_Intellij\\newman\\Collections\\api_collection.json', arr1, 'utf8', function (err) {
     if (err) return console.log(err);
  });

});
Share Improve this question edited Jul 25, 2019 at 13:30 Yahia 8458 silver badges26 bronze badges asked Nov 7, 2017 at 9:07 Saurav RathSaurav Rath 911 gold badge3 silver badges11 bronze badges 1
  • javascript does not support recursive matching for regexp natively, maybe you want to use a library for that – FrontTheMachine Commented Nov 7, 2017 at 9:13
Add a ment  | 

1 Answer 1

Reset to default 4

You need to do the second replace on the result of the first replace. data isn't changed by replace; replace returns a new string with the change.

So:

var result = data.replace(/{{Name}}/g, 'abc')
                 .replace(/{{Address}}/g, 'xyz');

...then write result to the file.

Alternately, and this particularly useful if it's a large file or you have lots of different things to replace, use a single replace with a callback:

var replacements = Object.assign(Object.create(null), {
    "{{Name}}": "abc",
    "{{Address}}": "xyz"
    // ...and so on...
});

var result = data.replace(/{{[^}]+}}/g, function(m) {
    return replacements[m] || m;
});

Or with a Map:

var replacements = new Map([
    ["{{Name}}", "abc"],
    ["{{Address}}", "xyz"]
    // ...and so on...
]);

var result = data.replace(/{{[^}]+}}/g, function(m) {
    return replacements.get(m) || m;
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745430119a4627352.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信