Replace a string in a file with nodejs

Asked
Active3 hr before
Viewed126 times

9 Answers

replacestring
90%

You could use simple regex:,I ran into issues when replacing a small placeholder with a large string of code.,Specify replacement options,Synchronous replacement:

You could use simple regex:

var result = fileAsString.replace(/string to be replaced/g, 'replacement');

So...

var fs = require('fs')
fs.readFile(someFile, 'utf8', function(err, data) {
   if (err) {
      return console.log(err);
   }
   var result = data.replace(/string to be replaced/g, 'replacement');

   fs.writeFile(someFile, result, 'utf8', function(err) {
      if (err) return console.log(err);
   });
});
load more v
88%

This callback provides for an extra argument above the String replace method, which is the name of the file in which the replacement is being performed. The following example replaces the matched string with the filename:,A simple utility to quickly replace text in one or more files or globs. Works synchronously or asynchronously with either promises or callbacks. Make a single replacement or multiple replacements at once.,Replace a single file or glob, numReplacements: Indicates the number of times a replacement was made in the file

# Using npm, installing to local project
npm i--save replace - in -file

# Using npm, installing globally
for global cli usage
npm i - g replace - in -file

# Using yarn
yarn add replace - in -file
load more v
72%

var fs = require('fs')
fs.readFile(someFile, 'utf8', function(err, data) {
   if (err) {
      return console.log(err);
   }
   var result = data.replace(/string to be replaced/g, 'replacement');

   fs.writeFile(someFile, result, 'utf8', function(err) {
      if (err) return console.log(err);
   });
});
65%

Example. It will be replacing the word email to a name in a text file index.txt with simple RegExp replace(/email/gim, 'name'), Determining the line count of a text file , Determining the line count of a text file , Reading a file into a Buffer using streams

Example. It will be replacing the word email to a name in a text file index.txt with simple RegExp replace(/email/gim, 'name')

var fs = require('fs');

fs.readFile('index.txt', 'utf-8', function(err, data) {
   if (err) throw err;

   var newValue = data.replace(/email/gim, 'name');

   fs.writeFile('index.txt', newValue, 'utf-8', function(err, data) {
      if (err) throw err;
      console.log('Done!');
   })
})
75%

How this can be done to search and replace in all files into a specify folder? 😎

How this can be done to search and replace in all files into a specify folder? 😎

search and replace
40%

As you can see, using a string as the substring to search for will only replace the first appearance. A regular expression with the /g suffix replaces all dashes with an empty string.,A regular expression instead of a string will replace all appearances. Pass it in as the first argument to string.replace():,The next section describes you how to replace all dash appearances in the string.,Now you may go ahead and use string.replace('-', ''). The problem: it replaces only the first dash because the method searches for the substring“-” and stops when found. It won’t replace the second, third, and other dashes in the string.

Now you may go ahead and use string.replace('-', ''). The problem: it replaces only the first dash because the method searches for the substring“-” and stops when found. It won’t replace the second, third, and other dashes in the string.

const string = 'e851e2fa-4f00-4609-9dd2-9b3794c59619'

console.log(string.replace('-', ''))
// -> e851e2fa4f00-4609-9dd2-9b3794c59619
load more v
22%

I use the md5 grunt task to generate MD5 filenames. Now I want to rename the sources in the HTML file with the new filename in the callback of the task. I wonder what's the easiest way to do this.,For windows you might fail to clean the cache, so you can remove the cache manually by deleting all the following folder content:,First, open the console as administrator (or in the terminal with sudo if it is a Linux) and:,Yarn option: Some people ended with the same issues even with the cleanup procedures, actually you save your time a lot using Yarn. Personally, I started to use yarn instead of traditional npm i, I can guarantee it is faster, and save your time, and your head from npm headaches.

You could use simple regex:

var result = fileAsString.replace(/string to be replaced/g, 'replacement');

So...

var fs = require('fs')
fs.readFile(someFile, 'utf8', function(err, data) {
   if (err) {
      return console.log(err);
   }
   var result = data.replace(/string to be replaced/g, 'replacement');

   fs.writeFile(someFile, result, 'utf8', function(err) {
      if (err) return console.log(err);
   });
});
load more v
60%

The replace() method searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced.,Note: If you are replacing a value (and not a regular expression), only the first instance of the value will be replaced. To replace all occurrences of a specified value, use the global (g) modifier (see "More Examples" below).,Read more about regular expressions in our RegExp Tutorial and our RegExp Object Reference.,replace() does not change the original string.

Definition and Usage

The replace() method searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced.

replace()
load more v
48%

The parameters for this function include the input file or file URL, the match string and its replacement string, and an option for matching the case of the replaced string.,Working with long text file can be tedious and time consuming, particularly when extra care must be made with sensitive documents like contracts and proposals where names, titles, and terms may need to be changed depending on the recipient. The following API will allow you to find and replace all instances of a text string in a DOCX document.,This will return an output file with the results of the text string replacement. You can retrieve the API Key from Cloudmersive at no cost and with no commitment. This will give you access to 800 monthly calls across our library of APIs.,There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

To use this API, run this command to install the SDK:

npm install cloudmersive - convert - api - client--save
load more v

Other "replace-string" queries related to "Replace a string in a file with nodejs"