Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
replace-in-file
Advanced tools
The replace-in-file npm package allows you to easily search and replace text in files. It is useful for tasks such as updating configuration files, modifying source code, and performing batch text replacements.
Basic Replacement
This feature allows you to perform a basic search and replace operation in a specified file. The 'from' field can be a string or a regular expression, and the 'to' field is the replacement string.
const replace = require('replace-in-file');
const options = {
files: 'path/to/file.txt',
from: /oldText/g,
to: 'newText',
};
replace(options)
.then(results => console.log('Replacement results:', results))
.catch(error => console.error('Error occurred:', error));
Multiple Replacements
This feature allows you to perform multiple search and replace operations in a single file. The 'from' and 'to' fields are arrays, where each element in 'from' is replaced by the corresponding element in 'to'.
const replace = require('replace-in-file');
const options = {
files: 'path/to/file.txt',
from: [/oldText1/g, /oldText2/g],
to: ['newText1', 'newText2'],
};
replace(options)
.then(results => console.log('Replacement results:', results))
.catch(error => console.error('Error occurred:', error));
Glob Pattern Matching
This feature allows you to use glob patterns to specify multiple files for the search and replace operation. This is useful for batch processing multiple files in a directory.
const replace = require('replace-in-file');
const options = {
files: 'path/to/*.txt',
from: /oldText/g,
to: 'newText',
};
replace(options)
.then(results => console.log('Replacement results:', results))
.catch(error => console.error('Error occurred:', error));
Dry Run
This feature allows you to perform a dry run of the search and replace operation. No actual changes are made to the files, but you can see what the results would be. This is useful for testing your replacement logic before applying it.
const replace = require('replace-in-file');
const options = {
files: 'path/to/file.txt',
from: /oldText/g,
to: 'newText',
dry: true,
};
replace(options)
.then(results => console.log('Dry run results:', results))
.catch(error => console.error('Error occurred:', error));
The 'replace' package is another tool for performing search and replace operations in files. It offers similar functionality to 'replace-in-file' but with a simpler API. It is suitable for straightforward replacement tasks.
The 'string-replace-loader' package is a webpack loader that allows you to perform string replacements in your source files during the build process. It is useful for replacing text in JavaScript and other files as part of your build pipeline.
The 'gulp-replace' package is a plugin for the Gulp task runner that allows you to perform search and replace operations in your Gulp streams. It integrates well with Gulp workflows and is useful for automating text replacements in your build process.
A simple utility to quickly replace text in one or more files.
npm install replace-in-file
var replace = require('replace-in-file');
//Asynchronous
replace({
//Single file
files: 'path/to/file',
//Or multiple files
files: [
'path/to/file',
'path/to/other/file',
],
//Replacement to make (can be string or regex)
replace: /Find me/g,
with: 'Replacement'
}, function(error, changedFiles) {
//Catch errors
if (error) {
return console.error('Error occurred:', error);
}
//List changed files
console.log('Modified files:', changedFiles.join(', '));
});
//Synchronous
try {
var changedFiles = replace({
//Single file
files: 'path/to/file',
//Or multiple files
files: [
'path/to/file',
'path/to/other/file',
],
//Replacement to make (can be string or regex)
replace: /Find me/g,
with: 'Replacement'
});
console.log('Modified files:', changedFiles.join(', '));
}
catch (error) {
console.error('Error occurred:', error);
}
(MIT License)
Copyright 2015-2016, Adam Buczynski
FAQs
A simple utility to quickly replace text in one or more files.
The npm package replace-in-file receives a total of 708,668 weekly downloads. As such, replace-in-file popularity was classified as popular.
We found that replace-in-file demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.