|
1 | | -# replace-regex |
2 | | -📂 TS compatible modern nodeJS find-and-replace in files with Regex & Glob support |
| 1 | +# replace-regex 📂 |
| 2 | + |
| 3 | +<a href="https://www.npmjs.com/package/replace-regex"><img src="https://img.shields.io/npm/v/replace-regex.svg" alt="Total Downloads"></a> |
| 4 | +<a href="https://www.npmjs.com/package/replace-regex"><img src="https://img.shields.io/npm/dw/replace-regex.svg" alt="Latest Stable Version"></a> |
| 5 | + |
| 6 | +📂 TS compatible modern nodeJS find-and-replace in files with Regex & Glob support. |
| 7 | + |
| 8 | +``` |
| 9 | +npm i replace-regex |
| 10 | +``` |
| 11 | + |
| 12 | +## Features |
| 13 | + |
| 14 | +- **Glob Support**: Use glob patterns to specify files. |
| 15 | +- **Regex Support**: Replace text using regular expressions. |
| 16 | +- **Dry Run**: Preview changes without modifying files. |
| 17 | +- **Counts**: Count matches and replacements. |
| 18 | +- **Customizable**: Pass custom options to fine-tune behavior. |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +### Basic Usage |
| 23 | + |
| 24 | +```ts |
| 25 | +import { replaceRegex } from 'replace-regex' |
| 26 | + |
| 27 | +const results = await replaceRegex({ |
| 28 | + files: 'src/**/*.js', |
| 29 | + from: /foo/g, |
| 30 | + to: 'bar', |
| 31 | + dry: false, |
| 32 | + countMatches: true, |
| 33 | +}) |
| 34 | + |
| 35 | +console.log(results) |
| 36 | +``` |
| 37 | + |
| 38 | +### Options |
| 39 | + |
| 40 | +- `files` (string or string[]): Glob patterns or file paths to process. |
| 41 | +- `from` (string | RegExp | (file: string) => string | RegExp): The pattern to search for. |
| 42 | +- `to` (string | (match: string, file: string) => string): The replacement string or function. |
| 43 | +- `dry` (boolean, optional): If true, no files will be overwritten. Default is `false`. |
| 44 | +- `ignore` (string[], optional): An array of glob patterns to exclude matches. |
| 45 | +- `disableGlobs` (boolean, optional): If true, disables glob pattern matching. Default is `false`. |
| 46 | +- `fastGlobOptions` (object, optional): Options to pass to fast-glob. |
| 47 | +- `countMatches` (boolean, optional): If true, counts the number of matches and replacements. Default is `false`. |
| 48 | + |
| 49 | +### Examples |
| 50 | + |
| 51 | +**Replace text in JavaScript files** |
| 52 | + |
| 53 | +```ts |
| 54 | +replaceRegex({ |
| 55 | + files: 'src/**/*.js', |
| 56 | + from: /console\.log/g, |
| 57 | + to: 'logger.log', |
| 58 | + dry: false, |
| 59 | + countMatches: true, |
| 60 | +}) |
| 61 | +``` |
| 62 | + |
| 63 | +**Dry Run** |
| 64 | + |
| 65 | +```ts |
| 66 | +const result = await replaceRegex({ |
| 67 | + files: 'src/**/*.js', |
| 68 | + from: /foo/g, |
| 69 | + to: 'bar', |
| 70 | + dry: true, // No files will be overwritten |
| 71 | + countMatches: true, |
| 72 | +}) |
| 73 | + |
| 74 | +console.log(`result → `, result) |
| 75 | +``` |
| 76 | + |
| 77 | +**Custom Replacement Function** |
| 78 | + |
| 79 | +```ts |
| 80 | +replaceRegex({ |
| 81 | + files: 'src/**/*.js', |
| 82 | + from: /foo/g, |
| 83 | + to: (match, file) => `${match.toUpperCase()} in ${file}`, |
| 84 | + dry: false, |
| 85 | + countMatches: true, |
| 86 | +}) |
| 87 | +``` |
0 commit comments