简体   繁体   中英

Javascript Find the next occurrence of a “letter”

I just need an idea!

If I have a string "string1, values, string2, values, string3, values, ....."

How can I get the index of the second string? why? because I want divide this string into

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

by using substring(0, index-1 of the second string) and so on.

To be more clear I have

""a", 4, 2, 2, 4, 5, "b", 6, 4, 3, 6, 7, "x", 5, 6, 7, 8, .... "

I want the "second" occurrence of a letter

It's not entirely clear to me what you're looking for, maybe this?

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] = [] 
})

This populates an object obj like this:

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

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