Get String Between Strings with Javascript
This Javascript function gets the substring located between the two strings passed as parameters, start and end. The start and end strings are not included in the returned string.
It can find the sub string between strings even if the large string has line returns.
function string_between_strings(startStr, endStr, str) {
pos = str.indexOf(startStr) + startStr.length;
return str.substring(pos, str.indexOf(endStr, pos));
}
Example:
Given the following sentence as the input string:
str = "With indoor climbing you will get both aerobic and anaerobic exercise engaging and working all muscle groups simultaneously";
start = " climbing ";
end = " engaging";
Usage:
between = string_between_strings(start, end, str);
between == "you will get both aerobic and anaerobic exercise ";