Skip to content

Instantly share code, notes, and snippets.

@GuillaumeJasmin
Last active September 26, 2022 04:10
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save GuillaumeJasmin/9119436 to your computer and use it in GitHub Desktop.
Save GuillaumeJasmin/9119436 to your computer and use it in GitHub Desktop.
Extract substring between two strings
String.prototype.extract = 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;
};
@monokaijs
Copy link

What about multiple matches?

@brilliant-ember
Copy link

Bless your soul!

@wacoom
Copy link

wacoom commented Dec 14, 2020

cool !

@CavalcanteLeo
Copy link

u rock

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment