Exciting release!Introducing "safe npm". Learn more
Socket
Log inDemoInstall

string-multiple-replace

Package Overview
Dependencies
0
Maintainers
1
Versions
15
Issues
File Explorer

Advanced tools

string-multiple-replace

Replace multiple substrings in a string in turn

    1.0.5latest
    Github

Version published
Maintainers
1
Weekly downloads
616
decreased by-40.54%

Weekly downloads

Changelog

Source

publish v1.0.5

  • format and rename test file;
  • update license;
  • fix readme;

Readme

Source

string-multiple-replace

Replace multiple substrings in a string in turn.

LICENSE npm-version npm

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

npm install --save string-multiple-replace

or

yarn add string-multiple-replace

Usage

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
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
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.

Keywords

FAQs

Last updated on 25 Oct 2021

Did you know?

Socket installs a Github app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.

Install Socket
Socket
support@socket.devSocket SOC 2 Logo

Product

  • Package Issues
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc