Last active
August 29, 2015 14:04
-
-
Save lucasfcosta/ceb89f6bd5d0f00a7aff to your computer and use it in GitHub Desktop.
Finds all occurrences of a String between two other Strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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