https://github.com/kodie/replace-once
Make multiple replacements in a string without replacing ones that have already been replaced.
https://github.com/kodie/replace-once
array find once replace string unique
Last synced: 11 months ago
JSON representation
Make multiple replacements in a string without replacing ones that have already been replaced.
- Host: GitHub
- URL: https://github.com/kodie/replace-once
- Owner: kodie
- License: mit
- Created: 2018-02-28T22:35:08.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T19:56:52.000Z (over 3 years ago)
- Last Synced: 2024-10-28T15:22:03.474Z (over 1 year ago)
- Topics: array, find, once, replace, string, unique
- Language: JavaScript
- Size: 4.88 KB
- Stars: 9
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- License: license.md
Awesome Lists containing this project
README
# replace-once
[](https://www.npmjs.com/package/replace-once)
[](https://travis-ci.org/kodie/replace-once)
[](https://www.npmjs.com/package/replace-once)
[](https://github.com/standard/standard)
[](license.md)
Make multiple replacements in a string without replacing ones that have already been replaced.
## Installation
```shell
npm install --save replace-once
```
## Usage
### replaceOnce(str, find, replace, [ flags ])
```javascript
const replaceOnce = require('replace-once')
var str = 'abc abcd a ab'
var find = ['abcd', 'abc', 'ab', 'a']
var replace = ['a', 'ab', 'abc', 'abcd']
replaceOnce(str, find, replace, 'gi')
//=> 'ab a abcd abc'
```
### Parameters
#### str (string)
The string to do replacements on.
#### find (array)
An array of strings to search for when doing replacements. Must be in the same order as their replacement specified inside of the `replace` parameter. Strings may contain [Regular Expressions](https://en.wikipedia.org/wiki/Regular_expression) (regexp).
#### replace (array)
An array of strings to replace the strings specified inside of the `find` parameter with. Must be in the same order as their counterpart specified inside of the `find` parameter.
#### flags (string)
*Optional*
RegExp flags to use when doing replacements. (e.g `g` for global, `i` for case-insensitive)
## License
MIT. See the [license.md file](license.md) for more info.