Skip to content

๐Ÿ“‚ TS compatible modern nodeJS find-and-replace in files with Regex & Glob support

License

Notifications You must be signed in to change notification settings

mesqueeb/replace-regex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

6 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

replace-regex ๐Ÿ“‚

Total Downloads Latest Stable Version

๐Ÿ“‚ TS compatible modern nodeJS find-and-replace in files with Regex & Glob support.

npm i replace-regex

Features

  • Glob Support: Use glob patterns to specify files.
  • Regex Support: Replace text using regular expressions.
  • Dry Run: Preview changes without modifying files.
  • Count: Count matches and replacements.
  • Customizable: Pass custom options to fine-tune behavior.

Usage

Basic Usage

import { replaceRegex } from 'replace-regex'

const results = await replaceRegex({
  files: 'src/**/*.js',
  from: /foo/g,
  to: 'bar',
  dry: false,
  countMatches: true,
})

console.log(results)

Options

  • files (string or string[]): Glob patterns or file paths to process.
  • from (string | RegExp | (file: string) => string | RegExp): The pattern to search for.
  • to (string | (match: string, file: string) => string): The replacement string or function.
  • dry (boolean, optional): If true, no files will be overwritten. Default is false.
  • ignore (string[], optional): An array of glob patterns to exclude matches.
  • disableGlobs (boolean, optional): If true, disables glob pattern matching. Default is false.
  • fastGlobOptions (object, optional): Options to pass to fast-glob.
  • countMatches (boolean, optional): If true, counts the number of matches and replacements. Default is false.

Examples

Replace text in JavaScript files

replaceRegex({
  files: 'src/**/*.js',
  from: /console\.log/g,
  to: 'logger.log',
  dry: false,
  countMatches: true,
})

Dry Run

const result = await replaceRegex({
  files: 'src/**/*.js',
  from: /foo/g,
  to: 'bar',
  dry: true, // No files will be overwritten
  countMatches: true,
})

console.log(`result โ†’ `, result)

Custom Replacement Function

replaceRegex({
  files: 'src/**/*.js',
  from: /foo/g,
  to: (match, file) => `${match.toUpperCase()} in ${file}`,
  dry: false,
  countMatches: true,
})

About

๐Ÿ“‚ TS compatible modern nodeJS find-and-replace in files with Regex & Glob support

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published