簡體   English   中英

Javascript查找下一個出現的“字母”

[英]Javascript Find the next occurrence of a “letter”

我只需要一個主意!

如果我有一個字符串"string1, values, string2, values, string3, values, ....."

如何獲取第二個字符串的索引? 為什么? 因為我想把這個字符串分成

"string1, values"
"string2, values"
...

通過使用substring(0, index-1 of the second string)等等。

更清楚地說,我有

“” a“,4,2,2,4,5,” b“,6,4,3,6,7,” x“,5,6,7,8,...。”

我想要字母的“第二”出現

我不清楚您要尋找什么,也許是這樣?

str = '"a", 4, 2, 2, 4, 5, "b", 6 4 3 6 7, "x", 5 6 7 8"'
obj = {}, last = ""
str.replace(/(\d+)|(\w+)/g, function(_, d, w) { 
     d ? obj[last].push(parseInt(d)) : obj[last = w] = [] 
})

這樣將填充對象obj如下所示:

{"a":[4,2,2,4,5],"b":[6,4,3,6,7],"x":[5,6,7,8]}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM