JSFiddle - React, Tailwind, and code Playground
JavaScript
var string = "?str!ing."; //input string
var arr = [0, 4, 8]; // indices of the punctuation signs in the string
var res = [];
if(arr[0] > 0) res.push(string.substring(0, arr[0])); //only needed if the first index is not the start of the string and the first substring has to be added
for(var i = 0; i< arr.length; i++){
var index = arr[i];
res.push(string.substring(index,++index)); //add the puncutation sign
var next = arr[i+1] || string.length; //get the next index (or end of string)
if(index<next) res.push(string.substring(index, next)); //add the substring if length > 0
}
console.log(res);