the code
var text1 = "abcdefgh";
var text2 = "cde";
alert(text1.substring(0, text1.indexOf(text2)));
alert(text1.substring(0, text1.indexOf(text2) + text2.length));
The first warning does not include search text, the second -.
Explanation
I will explain the second line of code.
text1.substring(0, text1.indexOf(text2) + text2.length))
text1.substring(startIndex, endIndex)
This piece of code takes every character from startIndex to endIndex, 0 is the first character. So, in our code we find from 0 (beginning) and end:
text1.indexOf(text2)
This returns the character position of the first instance of text2 in text 1.
text2.length
This returns the length of text 2, so if we want to include this in our return value, we add it to the length of the returned index, giving us the returned result!