Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript find all occurrences in string

var str = "I learned to play the Ukulele in Lebanon."
var regex = /le/gi, result, indices = [];
while ( (result = regex.exec(str)) ) {
    indices.push(result.index);
}
console.log(indices) // => [2, 25, 27, 33]
//find all occurence of le and return the return an array of the indeces
Comment

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
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: assign key and value to object 
Javascript :: object exists in array javascript 
Javascript :: Import file to mongodb database 
Javascript :: vscode js brackets not close 
Javascript :: javascript random word 
Javascript :: get last item in array 
Javascript :: two decimel javascript 
Javascript :: how to clear local storage 
Javascript :: Facebook passport Oauth authenticate strategy 
Javascript :: javascript merge two objects 
Javascript :: es6 loop through object 
Javascript :: dropzone on success all files 
Javascript :: reactnative get height screen 
Javascript :: how-to-save-array in Local storage - js 
Javascript :: javascript es6 check if index exists 
Javascript :: javascript form submit on button click check if required fields not empty 
Javascript :: do while loop 
Javascript :: To append dropdown option using jquery 
Javascript :: compose javascript 
Javascript :: keypress event 
Javascript :: javascript array concat spread operator 
Javascript :: javascript write all the prime numbers from 1 to 100 
Javascript :: eject expo app to android and react native 
Javascript :: javascript append to array 
Javascript :: datatables dynamically hide columns 
Javascript :: error handling in express 
Javascript :: jquery change input value if greater than 
Javascript :: To split a full name into first and last names in JavaScript 
Javascript :: navigation.openDrawer is not a function react native 
Javascript :: javascript add listeners to class 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =