[英]Check First Character String
我對這個任務有點困惑。
給定 list="aaabcdefaabc" 和 word="abc"。
您可以看到包含“abc”的列表將使我們的答案為 2 和 9
你能給我一些關於 javascript 的見解嗎?
你的意思是這樣的嗎?
let list="aaabcdefaabc"
matches = list.matchAll("abc")
for (let match of matches) console.log(match.index) // prints 2 and 9
編輯:制作一個 function 返回索引
在這個答案的幫助下:
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)
我不確定我是否理解這個問題。
可以使用 indexOf 和 lastIndexOf 找到索引。
let list="aaabcdefaabc"
console.log(list.indexOf("abc"))
// this print 2
console.log(list.lastIndexOf("abc"))
// this print 9
更多文檔可以在這里找到, https://www.w3schools.com/jsref/jsref_lastindexof.asp
這對你有幫助嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.