Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

js find all string occurrences

function getIndicesOf(searchStr: string, str: string, caseSensitive?: boolean) {
  const searchStrLen = searchStr.length
  if (searchStrLen === 0) {
    return []
  }
  let startIndex = 0
  let index: number
  const indices: number[] = []

  if (!caseSensitive) {
    str = str.toLowerCase()
    searchStr = searchStr.toLowerCase()
  }
  // eslint-disable-next-line no-cond-assign
  while ((index = str.indexOf(searchStr, startIndex)) > -1) {
    indices.push(index)
    startIndex = index + searchStrLen
  }
  return indices
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #js #find #string #occurrences
ADD COMMENT
Topic
Name
9+1 =