简体   繁体   中英

Check First Character String

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM