Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including nth-indexof with all npm packages installed. Try it out:

var nthIndexof = require("nth-indexof")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

nth-indexof v1.0.0

Find the nth position of a pattern in a string

nth-indexof

Find the nth position of a pattern in a string.

Similar to str.indexOf(pattern) except that you can choose how many occurrences of pattern you wish to skip before getting the index.

Build status js-standard-style

Installation

npm install nth-indexof --save

Usage

const indexOf = require('nth-indexof')

const text = 'foo\nbar\nbaz'

console.log('The index of the 2nd linebreak is:', indexOf(text, '\n', 1)) // => 7

API

index = indexOf(haystack, needle[, skip][, offset])

Returns the index of the needle in the haystack ignoring the first skip occurrences indicated.

Arguments:

  • haystack - A string in which to search
  • needle - The string to search for
  • skip - Optional number of instances of needle to skip before finding the next index (default: 0)
  • offset - Optional offset in the haystack from where to start the search

Returns -1 if no match is found.

License

MIT

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free