🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

node-replace

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-replace

Command line search and replace utility

0.3.3
latest
Source
npm
Version published
Weekly downloads
1.2K
-25.4%
Maintainers
1
Weekly downloads
 
Created
Source

node-replace

Maintaned Fork from harthur/replace

replace is a command line utility for performing search-and-replace on files. It's similar to sed but there are a few differences:

  • Modifies files when matches are found
  • Recursive search on directories with -r
  • Uses JavaScript syntax for regular expressions and replacement strings.

Install

With node.js and npm:

npm install node-replace -g

You can now use replace and search from the command line.

Examples

Replace all occurrences of "foo" with "bar" in files in the current directory:

replace 'foo' 'bar' *

Replace in all files in a recursive search of the current directory:

replace 'foo' 'bar' . -r

Replace only in test/file1.js and test/file2.js:

replace 'foo' 'bar' test/file1.js test/file2.js

Replace all word pairs with "_" in middle with a "-":

replace '(\w+)_(\w+)' '$1-$2' *

Replace only in files with names matching *.js:

replace 'foo' 'bar' . -r --include="*.js"

Don't replace in files with names matching *.min.js and *.py:

replace 'foo' 'bar' . -r --exclude="*.min.js,*.py"

Preview the replacements without modifying any files:

replace 'foo' 'bar' . -r --preview

See all the options:

replace -h

There's also a search command. It's like grep, but with replace's syntax.

search "setTimeout" . -r

Programmatic Usage

You can use replace from your JS program:

var replace = require("node-replace");

replace({
  regex: "foo",
  replacement: "bar",
  paths: ['.'],
  recursive: true,
  silent: true,
});

More Details

Excludes

By default, replace and search will exclude files (binaries, images, etc) that match patterns in the "defaultignore" located in this directory.

On huge directories

If replace is taking too long on a large directory, try turning on the quiet flag with -q, only including the necessary file types with --include or limiting the lines shown in a preview with -n.

What it looks like

replace

Keywords

sed

FAQs

Package last updated on 24 Oct 2017

Did you know?

Socket

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.

Install

Related posts