I'm a little bit confused with this task.
given the list="aaabcdefaabc" and word="abc".
You can see that list contain "abc" will make our answer 2 and 9
Can you give me some insight in javascript ?
I'm a little bit confused with this task.
given the list="aaabcdefaabc" and word="abc".
You can see that list contain "abc" will make our answer 2 and 9
Can you give me some insight in javascript ?
do you mean something like this?
let list="aaabcdefaabc"
matches = list.matchAll("abc")
for (let match of matches) console.log(match.index) // prints 2 and 9
EDIT: make a function returning the indices
With the help of this answer:
function searchIndices(str,pattern) {
let matches = str.matchAll(pattern) // Object [RegExp String Iterator]
matches = [...matches] // convert it to Array
indices = matches.map(match => match.index) // get index from each match
return indices
}
let ii = searchIndices("aaabcdefaabc","abc")
console.log(ii)
I'm not sure I understand the question.
Index could be found with indexOf and lastIndexOf.
let list="aaabcdefaabc"
console.log(list.indexOf("abc"))
// this print 2
console.log(list.lastIndexOf("abc"))
// this print 9
More documentation could be found here, https://www.w3schools.com/jsref/jsref_lastindexof.asp
This help you?