Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jonschlinkert/replace-case

Like String.prototype.replace() but attempts to match the casing of the substring being replaced. Useful when renaming mixed-case variables, refactoring code or moving hard-coded values into variables.
https://github.com/jonschlinkert/replace-case

case case-sensitive match regex regexp replace replacement string

Last synced: 2 months ago
JSON representation

Like String.prototype.replace() but attempts to match the casing of the substring being replaced. Useful when renaming mixed-case variables, refactoring code or moving hard-coded values into variables.

Awesome Lists containing this project

README

        

# replace-case [![NPM version](https://img.shields.io/npm/v/replace-case.svg?style=flat)](https://www.npmjs.com/package/replace-case) [![NPM monthly downloads](https://img.shields.io/npm/dm/replace-case.svg?style=flat)](https://npmjs.org/package/replace-case) [![NPM total downloads](https://img.shields.io/npm/dt/replace-case.svg?style=flat)](https://npmjs.org/package/replace-case) [![Tests](https://github.com/jonschlinkert/replace-case/actions/workflows/test.yml/badge.svg)](https://github.com/jonschlinkert/replace-case/actions/workflows/test.yml)

> Like String.prototype.replace() but attempts to match the casing of the substring being replaced.

Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.

## Install

Install with [npm](https://www.npmjs.com/):

```sh
$ npm install --save replace-case
```

## Usage

```js
const replace = require('replace-case');
const input = 'foo Foo FOO foo';

// Uses "gi" RegExp flags by default
console.log(replace(input, 'foo', 'bar')); //=> bar Bar BAR bar
console.log(replace(input, '[fF]oo', 'bar', 'g')); //=> bar Bar FOO bar
console.log(replace(input, 'foo', 'bar', 'i')); //=> bar Foo FOO foo
console.log(replace(input, 'foo', 'bar', '')); //=> bar Foo FOO foo
```

**Signature**

```js
replace(input, substring, replacement[, flags]);
```

**Params**

* `input` (String) The string to modify
* `substring` (String) The "old" string to replace
* `replacement` (String) The "new" string to use as a replacement.
* `flags` (String) Optional [RegExp flags](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Advanced_searching_with_flags_2) to use. By default, `gi` is used.

## Usage Examples

```js
const input = `
.alphaconfig.json
AlphaWord
Alpha
ALPHA_FOO_BAR
`;

// Replace all occurrences of "alpha" with "beta"
console.log(replace(input, 'alpha', 'beta'));
// Replace only the first occurrence of "alpha" with "beta"
console.log(replace(input, 'alpha', 'beta', 'i'));
// Replace only when surrounded by word boundaries (capture group is unecessary and is only for clarity)
console.log(replace(input, '\\b(?:alpha)\\b', 'beta'));
// Replace all occurrences of "alpha" (optionally followed by "config") with "beta"
console.log(replace(input, 'alpha(config)*', 'beta'));
// Replace all occurrences of "alpha" (optionally followed by zero or more characters that are
// not a space, underscore, or dot)
console.log(replace(input, 'alpha([^\\s_.]*)', 'beta'));
```

Results in the following:

```
.betaconfig.json
BetaWord
Beta
BETA_FOO_BAR

.betaconfig.json
AlphaWord
Alpha
ALPHA_FOO_BAR

.alphaconfig.json
AlphaWord
Beta
ALPHA_FOO_BAR

.beta.json
BetaWord
Beta
BETA_FOO_BAR

.beta.json
beta
Beta
BETA_FOO_BAR
```

## About

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).

Please read the [contributing guide](.github/contributing.md) for advice on opening issues, pull requests, and coding standards.

Running Tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

```sh
$ npm install && npm test
```

Building docs

_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_

To generate the readme, run the following command:

```sh
$ npm install -g verbose/verb#dev verb-generate-readme && verb
```

### Author

**Jon Schlinkert**

* [GitHub Profile](https://github.com/jonschlinkert)
* [Twitter Profile](https://twitter.com/jonschlinkert)
* [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)

### License

Copyright © 2023, [Jon Schlinkert](https://github.com/jonschlinkert).
Released under the MIT License.

***

_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on November 03, 2023._