1

I have a text content as follows:

var text = "asd^dfdf^dsfgdsfg^zdfsd^sdfsdf^dfsdf^dsfsdf^dfsdf^dfsf^sdfsdf^..."

I want to split the text up-to multiple of 5th occurrence of ^ and pass to a function with the ^ symbol.

If there are 31 ^ in the element then: 5th, 10th, 15th, 20th, 25th, 30th and then remaining should be passed to the function (i.e. 31st with ^).

I prefer a for loop like:

var spl = text.split(); //up-to 5th multiple

for(i=0; i<spl.length; i++){
 passfun(upto 5th^ with cap symbol)
}

Example:

var text = "asd^dfdf^dsfgdsfg^zdfsd^sdfsdf^dfsdf^dsfsdf^dfsdf^dfsf^sdfsdf^sssad^gsds..."

passfun("asd^dfdf^dsfgdsfg^zdfsd^sdfsdf^"); //1st time
passfun("dfsdf^dsfsdf^dfsdf^dfsf^sdfsdf^");//2nd time
passfun("sssad^gsds");//last
Minhas Kamal
  • 20,752
  • 7
  • 62
  • 64
Santhucool
  • 1,656
  • 2
  • 36
  • 92

3 Answers3

2

Try something like this

var text = "asd^dfdf^dsfgdsfg^zdfsd^sdfsdf^dfsdf^dsfsdf^dfsdf^dfsf^sdfsdf^...";
var spl = text.split('^');
Array.prototype.chunk = function ( n ) {
    if ( !this.length ) {
        return [];
    }
    return [ this.slice( 0, n ) ].concat( this.slice(n).chunk(n) );
};
console.log(spl.chunk(5)[0].join('^')+'^');

more info Split array into chunks

demo here: https://jsfiddle.net/bo4eacv5/1/

Community
  • 1
  • 1
madalinivascu
  • 32,064
  • 4
  • 39
  • 55
2

You can try this:

var text = "asd^dfdf^dsfgdsfg^zdfsd^sdfsdf^dfsdf^dsfsdf^dfsdf^dfsf^sdfsdf",
    temp = text.split('^');
for (i = 0; i < temp.length; i += 5) {
  passfun(temp.slice(i, 5 + i).join('^') + ( i + 5 < temp.length ? '^' : ''));
}
Abhishekh Gupta
  • 6,206
  • 4
  • 18
  • 46
0

Hello friend it perfect little bit may be length correction need if you have any problem then otherwise no need

var text = "asd^dfdf^dsfgdsfg^zdfsd^sdfsdf^dfsdf^dsfsdf^dfsdf^dfsf^sdfsdf^split^text^upto^5th^and^..";
                //I want to split the text upto multiple of 5th occurrance of ^ and
                //var abc =  text.split("^").length - 1;
                var counter = 0, index = 0;
                var temp = 0, indexArr = [];
                for (var i = 0; i < text.length; i++) {
                    index++;
                    if (text.charAt(i) == '^') {
                        counter++;
                    }
                    if (counter == 5) {
                        counter = 0;
                        myString(index);
                    }
                }
                function myString(tempindex) {
                    var tempString = '';
                    indexArr[temp] = tempindex;
                    if (temp == 0) {
                        tempString = text.substr(0, tempindex);
                        passfun(tempString);
                        temp++;
                    } else {
                        tempString = text.substr(indexArr[temp - 1], indexArr[temp - 1]);
                        passfun(tempString);
                        temp++;
                    }
                    return;
                }
Rajiv
  • 78
  • 10