/ Published in: JavaScript
//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 "
//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 "
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
//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; }