Javascript Getbetween Function Multiple Instances
function getBetween(content, start, end) { var r = content.split(start); if (1 in r) { var z = r[1].split(end); return z[0]; } return ''; } Hello,
Solution 1:
You can use this function:
functiongetBetween(content, start, end) {
var arr = [];
content.replace(newRegExp(start + "(.+)" + end,"g"),function(m,g1){ arr.push(g1); return'';});
return arr;
}
var output = getBetween('I am 30 years old I am 20 years old I am 50 years old', 'I am ', ' years old');
Output:
["30", "20", "50"]
Solution 2:
Just try with:
functiongetBetween(content, start, end) {
var result = [];
var r = content.split(start);
for (var i = 1; i < r.length; i++) {
result.push(r[i].split(end)[0]);
}
return result;
}
var output = getBetween('I am 30 years old I am 20 years old I am 50 years old', 'I am ', ' years old');
Output:
["30", "20", "50"]
Post a Comment for "Javascript Getbetween Function Multiple Instances"