🚀 Launch Week Day 5:Introducing Immutable Scans.Learn More →
Socket
Book a DemoInstallSign in
Socket

replace-nth

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

replace-nth

Replace only specific occurrences of a string. e.g. if the string exists 10 times, you can replace, say, the 3rd and 8th occurrences only, etc. Returns a new string with nth specific matches of a pattern replaced by a replacement. The pattern can be a str

latest
Source
npmnpm
Version
0.2.1
Version published
Maintainers
1
Created
Source

replace-nth NPM version

Replace only specific occurrences of a string. e.g. if the string exists 10 times, you can replace, say, the 3rd and 8th occurrences only, etc. Returns a new string with nth specific matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match.

Install

Install with npm

npm i replace-nth --save

Tests

Run the tests:

npm test

See the tests for examples.

Usage

var replaceNth = require('replace-nth');
replaceNth( params );

Params:

  • num (string): a number formatted as a string.
  • pattern (regex|string): The actual pattern to find and replace nth times
  • replacement (string): the replacement to use
  • str (string): the string to search for replacements

Example:

var params = {
  // pass any string
  str: 'aaaaaaaaaaaaaaa'
  // The nth number of match(es) to replace
  num: '[135]',
  // The pattern to replace
  pattern: /a/g,
  // the replacement to use
  replacement: 'B',
};
console.log(replaceNth(params));
//=> 'BaBaBaaaaaaaaaa'

num

  • '12' returns twelvth match,
  • '[124]' returns the first, second and fourth matchs, and so on.

Author

Jon Schlinkert

License

Copyright (c) 2014 Jon Schlinkert, contributors.
Released under the MIT license

This file was generated by verb-cli on October 04, 2014.

Keywords

expression

FAQs

Package last updated on 04 Oct 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts