JSPM

indicesof

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 8
  • Score
    100M100P100Q58375F
  • License GPLv3+

Just like indexOf(), but returns an array of all occurrences. Works on both strings and buffers.

Package Exports

  • indicesof

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (indicesof) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

js-indicesof

Just like indexOf(), but returns an array of all occurrences.

Will return an empty array if there is no match.

Install

npm i indicesof

Syntax

function (needle, haystack, startOffset, endOffset ]);
  • haystack can be a string or a buffer.
  • startOffset is optional. Default is 0.
  • endOffset is optional. Default is the haystack length. Note that the entire length of the needle must be within the offsets to be a match. Also, the offset is not inclusive.

Strings vs Buffers

Note that the byte position is returned for Buffers, which might not be the same as the char position for strings:

indicesOf('l', 'Älg'); // Returns [ 1 ]
indicesOf('l', Buffer.from('Älg', 'utf8')); // Returns [ 2 ]

Example

var indicesOf = require('indicesof');
var haystack = "If I Can't Dance It's Not My Revolution";
indicesOf('n', haystack); // [ 7, 13, 38 ]
indicesOf('an', haystack); // [ 6, 12 ]
indicesOf('Dan', haystack); // [ 11 ]
indicesOf('dan', haystack); // [ ]