Skip to content

Instantly share code, notes, and snippets.

@lucasfcosta
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucasfcosta/ceb89f6bd5d0f00a7aff to your computer and use it in GitHub Desktop.
Save lucasfcosta/ceb89f6bd5d0f00a7aff to your computer and use it in GitHub Desktop.
Finds all occurrences of a String between two other Strings
function findWithin(text, first, last, caseSensitive) {
if (!caseSensitive) {
text = text.toLowerCase();
first = first.toLowerCase();
last = last.toLowerCase();
}
var rex = new RegExp(first + '(.*?)' + last, 'gm'),
results = text.match(rex);
for (var i in results) {
results[i] = results[i].slice(first.length, results[i].indexOf(last));
}
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment