简体   繁体   English

检查第一个字符串

[英]Check First Character String

I'm a little bit confused with this task.我对这个任务有点困惑。

given the list="aaabcdefaabc" and word="abc".给定 list="aaabcdefaabc" 和 word="abc"。

You can see that list contain "abc" will make our answer 2 and 9您可以看到包含“abc”的列表将使我们的答案为 2 和 9

Can you give me some insight in javascript?你能给我一些关于 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编辑:制作一个 function 返回索引

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.可以使用 indexOf 和 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更多文档可以在这里找到, https://www.w3schools.com/jsref/jsref_lastindexof.asp

This help you?这对你有帮助吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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