Search code examples
javascriptstring-matching

Get text between 2 or more substrings


This is my first question, so excuse me if I mess something up. I'm new. Anywho, recently I've been designing a function which takes a string and 2 substrings, and then returns an array of the positions of both substrings so I can later substring the actual string using the positions of the substrings I'm searching for. I hope that makes sense.

function m(s,s1,s2,prevTable){
var a = prevTable || []
if (s.indexOf(s1) > -1 && s.indexOf(s2, s.indexOf(s1)) > -1){
    a.push([s.indexOf(s1),s.indexOf(s2, s.indexOf(s1))+s2.length])
    s=s.substring(s.indexOf(s2, s.indexOf(s1)+s2.length))
    console.log(s)
    m(s,s1,s2,a);
}
return a;
}

So to summarize it makes an array (a), finds the position of s1 and s2 (plus it's own length so it includes s2) in the source string (s), adds them to the array as it's own array. E.g a would be: a=[[2,5]], deletes up to where s2 was found (+s2.length to include s2), and then repeats it with the new string unless it can't find both s1 and s2, in which case it returns a.

However, it does not work as I intended it to. Upon running this:

var s = "Hey. This is pointless. Middle is always neutral. This is not 
pointless."
var a=m(s,"This","pointless.")
for (i=0;i<a.length;i++){
console.log(s.substring(a[i][0],a[i][1]))
}

The result I get is:

This is pointless.
dle is always neutral.

When I am expecting:

This is pointless.
This is not pointless.

Also, is there a name for this technique?


Solution

  • What you are trying to do could be accomplished more easily using regular expressions (MSDN Docs).

    Here is a simple example, note: I threw this together quickly, it may not handle all input perfectly.

    function splitBetweenTwoStrings(str, s1, s2){
      var reg = new RegExp("("+s1+".*?"+s2+")", "g");
      var result = [];
      var r = null;
      
      //get all instances and push into result array
      while((r=reg.exec(str))){
        result.push(r[1]);
      }
      
      return result;
    }
    
    console.log(splitBetweenTwoStrings("Hey. This is pointless. Middle is always neutral. This is not pointless.","This","pointless."))