KAZ-to-USA KAZ-to-USA - 6 months ago 24
Javascript Question

Javascript: Split with more than one parameters

Is there a function in Java Script that divide a string with several arguments?

var string = "This is a test text, again, this is a testing text";

For example, I can split with
by saying
and I will have:

var string = ["This is a test text", "again", "this is a testing text"];

Now, I want to split it with several parameters, so string now would be

var string = ["test text", "testing text"]

I'm looking for a function that extract all the parts that start with
and end with


I'm not sure that I understood what you want, but here is a function I wrote in 2 minutes. With the following scenario

var string = "This is a test text, again, this is a testing text";

function customSplit(str_to_split, start_string, end_string) {
    var res = [];
    var start_index, end_index;

    for (i = 0; i <= str_to_split.length; i++) {
        start_index = str_to_split.toLowerCase().indexOf(start_string, i);
        if (i == start_index) {
            end_index = str_to_split.indexOf(end_string, i);
            if (end_index >= 0) {
                res.push(str_to_split.substring(start_index, end_index + end_string.length));

    return res;

console.log(customSplit(string, "test", "text"));

it will output ["test text", "testing text"].

Let me know if it helps you.


Corrected a wrong behave with a particular string. Please remind that I wrote it in a couple of minutes.