https://github.com/ichengbo/string-multiple-replace
Replace multiple substrings in a string sequentially. 用于批量替换字符串,可以实现不覆盖替换和覆盖替换,具体可以参考readme例子。
https://github.com/ichengbo/string-multiple-replace
mutiple replace string
Last synced: 9 months ago
JSON representation
Replace multiple substrings in a string sequentially. 用于批量替换字符串,可以实现不覆盖替换和覆盖替换,具体可以参考readme例子。
- Host: GitHub
- URL: https://github.com/ichengbo/string-multiple-replace
- Owner: iChengbo
- License: mit
- Created: 2018-10-10T02:59:30.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-08-09T05:49:36.000Z (over 1 year ago)
- Last Synced: 2025-04-24T01:09:47.546Z (9 months ago)
- Topics: mutiple, replace, string
- Language: JavaScript
- Homepage:
- Size: 31.3 KB
- Stars: 3
- Watchers: 0
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# string-multiple-replace
> Replace multiple substrings in a string sequentially.
[](./LICENSE)
[](https://www.npmjs.com/package/string-multiple-replace)
[](https://www.npmjs.com/package/string-multiple-replace)
Replace all substring matches in a string with an mapping object that is table of `replaceThis: withThis`, and you can provide a `sequencer` to decide the order of replacement.
## Install
```sh
npm install --save string-multiple-replace
```
or
```sh
yarn add string-multiple-replace
```
## Usage
```js
const multiReplace = require('string-multiple-replace');
const input = "abcde";
const matcherObj = {
"a": "b",
"b": "c",
"c": "d",
"d": "e"
}
multiReplace(input, matcherObj, ["a", "b", "c", "d"]); // eeeee
multiReplace(input, matcherObj); // bcdee
```
- Example-1
```js
const multiReplace = require('string-multiple-replace');
const input = "I'm only brave when I have to be. Being brave doesn't mean you go looking for trouble.";
const matcherObj = {
"brave": "cowardly",
"trouble": "escape"
}
const sequencer = ["brave", "trouble"];
multiReplace(input, matcherObj, sequencer);
//I'm only cowardly when I have to be. Being cowardly doesn't mean you go looking for escape.
```
- Example-2
```js
const multiReplace = require('string-multiple-replace');
const input = "I'm only brave when I have to be. Being brave doesn't mean you go looking for trouble.";
const matcherObj = {
"brave": "cowardly",
"trouble": "escape"
}
multiReplace(input, matcherObj, keys => keys);
//I'm only cowardly when I have to be. Being cowardly doesn't mean you go looking for escape.
```
## API
> multiReplace(input, matcherObj [,sequencer])
The original string is replaced in turn according to the `matcherObj`, where `sequencer` determines the replacement order, and the existence state of `sequencer` determines whether the last operation overwrites the previous operation.
#### input
Type: `string`
*Required*
> A string to be processed.
#### matcherObj
Type: `object`
*Required*
> An object that represents a string replacement mapping.
#### sequencer
Type: `function`, `array`
*Required:false*
A `function` that takes the keys of `matcherObj`, and return an suquence array.
> Upgrade Instruction: the existence state of `sequencer` determines whether the last operation overwrites the previous operation.