Skip to content Skip to sidebar Skip to footer

Javascript Split String By Another String And Include That Another String That Was Used To Split It Into Result

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

Solution 1:

You can use regex and capture the split pattern:

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

var s = "ring";
var p = newRegExp("(" + s + ")")
console.log("MyStringForSplitting".split(p))

Post a Comment for "Javascript Split String By Another String And Include That Another String That Was Used To Split It Into Result"