How can I match overlapping strings with regex?

后端 未结 6 1749
猫巷女王i
猫巷女王i 2020-11-22 03:27

Let\'s say I have the string

\"12345\"

If I .match(/\\d{3}/g), I only get one match, \"123\". Why don\'t I get

6条回答
  •  失恋的感觉
    2020-11-22 03:29

    I would consider not using a regex for this. If you want to split into groups of three you can just loop over the string starting at the offset:

    let s = "12345"
    let m = Array.from(s.slice(2), (_, i) => s.slice(i, i+3))
    console.log(m)

提交回复
热议问题