Return to Snippet

Revision: 70890
at August 30, 2016 09:17 by Citylogic


Initial Code
//Takes 3 params
//1) inString: string to search
//2) inThis: aka find this / the starting text
//3) inThat: the ending string.
//For example: myBetween("the cow jumped over the moon", "cow", "moon")
//will return " jumped over the "
function myBetween(inString, inThis, inThat)
{
 var sResult = null; //return string	
 try
 {
  var inThisPos = inString.indexOf(inThis); //check if 'inThis' is found in 'inString'
  if (inThisPos > -1) //yes there's a match
  {
   var inThatPos = inString.indexOf(inThat, (inThisPos + inThis.length) + 1); //find the 'inThat'
   if (inThatPos > inThisPos) //was it past the start of inThis
   {
    sResult = inString.substr(inThisPos + inThis.length, inThatPos - (inThisPos + inThis.length));
   }
  }
 }
 catch(err)
 {
  console.log("[myBetween()] Error: " + err.message);
 }	
 return sResult;
}

Initial URL

                                

Initial Description
//Takes 3 params
//1) inString: string to search
//2) inThis: aka find this / the starting text
//3) inThat: the ending string.
//For example: myBetween("the cow jumped over the moon", "cow", "moon")
//will return " jumped over the "

Initial Title
Return a String Between 2 Strings

Initial Tags

                                

Initial Language
JavaScript