Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
gulp-replace
Advanced tools
The gulp-replace npm package is a plugin for Gulp, a streaming build system, that allows you to search and replace text in files. It is useful for tasks such as modifying configuration files, updating version numbers, or replacing placeholders in templates.
Basic String Replacement
This feature allows you to replace a specific string with another string in the files matched by the gulp.src() method. In this example, all occurrences of 'foo' in text files within the 'src' directory are replaced with 'bar' and the modified files are saved to the 'dist' directory.
const gulp = require('gulp');
const replace = require('gulp-replace');
gulp.task('replace-text', function() {
return gulp.src('src/*.txt')
.pipe(replace('foo', 'bar'))
.pipe(gulp.dest('dist'));
});
Regular Expression Replacement
This feature allows you to use regular expressions for more complex search and replace operations. In this example, any string matching the pattern 'foo' followed by one or more digits is replaced with 'bar'.
const gulp = require('gulp');
const replace = require('gulp-replace');
gulp.task('replace-regex', function() {
return gulp.src('src/*.txt')
.pipe(replace(/foo\d+/g, 'bar'))
.pipe(gulp.dest('dist'));
});
Replacement with a Function
This feature allows you to use a function to determine the replacement value. The function receives the matched string and can return a modified version of it. In this example, all occurrences of 'foo' are replaced with 'FOO'.
const gulp = require('gulp');
const replace = require('gulp-replace');
gulp.task('replace-function', function() {
return gulp.src('src/*.txt')
.pipe(replace('foo', function(match) {
return match.toUpperCase();
}))
.pipe(gulp.dest('dist'));
});
gulp-string-replace is another Gulp plugin that provides similar functionality for string replacement. It allows for both simple string replacements and replacements using regular expressions. Compared to gulp-replace, it offers a more straightforward API but lacks some of the advanced features like replacement with a function.
gulp-replace-task is a Gulp plugin designed for replacing text in files using a configuration object. It supports both string and regular expression replacements and allows for more complex replacement tasks through the use of configuration files. It is more suitable for projects that require extensive and configurable replacement tasks compared to gulp-replace.
gulp-batch-replace is a Gulp plugin that allows for batch replacements in files. It supports multiple replacements in a single pass and can handle both string and regular expression replacements. This package is useful for projects that need to perform multiple replacements efficiently, whereas gulp-replace is more focused on individual replacement tasks.
A string replace plugin for gulp 3
First, install gulp-replace
as a development dependency:
npm install --save-dev gulp-replace
Then, add it to your gulpfile.js
:
var replace = require('gulp-replace');
gulp.task('templates', function(){
gulp.src(['file.txt'])
.pipe(replace(/foo(.{3})/g, '$1foo'))
.pipe(gulp.dest('build/file.txt'));
});
gulp-replace can be called with a string or regex.
Type: String
The string to search for.
Type: String
or Function
The replacement string or function. If replacement
is a function, it will be called once for each match and will be passed the string that is to be replaced.
Note: gulp-replace cannot perform regex replacement on streams.
Type: RegExp
The regex pattern to search for. See the MDN documentation for RegExp for details.
Type: String
or Function
The replacement string or function. See the MDN documentation for String.replace for details.
An optional third argument, options
, can be passed.
Type: Object
Type: boolean
Default: false
Skip binary files
FAQs
A string replace plugin for gulp
The npm package gulp-replace receives a total of 198,487 weekly downloads. As such, gulp-replace popularity was classified as popular.
We found that gulp-replace demonstrated a not healthy version release cadence and project activity because the last version was released 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’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.