Skip to content

Instantly share code, notes, and snippets.

@carbonrobot
Created September 4, 2012 18:57
Show Gist options
  • Select an option

  • Save carbonrobot/3625017 to your computer and use it in GitHub Desktop.

Select an option

Save carbonrobot/3625017 to your computer and use it in GitHub Desktop.
Javascript string between
/**
* Usage var you = 'hello you guys'.between('hello ',' guys');
* you = 'you';
*/
String.prototype.between = function (prefix, suffix) {
s = this;
var i = s.indexOf(prefix);
if (i >= 0) {
s = s.substring(i + prefix.length);
}
else {
return '';
}
if (suffix) {
i = s.indexOf(suffix);
if (i >= 0) {
s = s.substring(0, i);
}
else {
return '';
}
}
return s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment