0

If I have this string: "MyStringToForSplitting" and I want to split it by this word "ring". If I do this: "MyStringToForSplitting".split('ring') I will get array that contains this elements:

  1. MyS
  2. ToForSplitting

I can't figure out best way to split it and include 'ring' in resulting array like this:

1.MyS

2.ring

3.ForSplitting

Vlado Pandžić
  • 4,879
  • 8
  • 44
  • 75

1 Answers1

6

You can use regex and capture the split pattern:

console.log("MyStringToForSplitting".split(/(ring)/));

var s = "ring";
var p = new RegExp("(" + s + ")")
console.log("MyStringForSplitting".split(p))
Psidom
  • 209,562
  • 33
  • 339
  • 356