Skip to content

Instantly share code, notes, and snippets.

@westc
Last active August 29, 2015 14:05
Show Gist options
  • Save westc/e08687b699784f6e46ff to your computer and use it in GitHub Desktop.
Save westc/e08687b699784f6e46ff to your computer and use it in GitHub Desktop.
Gets the substring before the specified target.
/**
* @license Copyright 2015 - Chris West - MIT Licensed
* Gets the substring before the specified target.
* @param {string|RegExp} target
* Specifies the substring to key off in order to pull the substring that
* precedes it.
* @param {number=} opt_occurrence
* Optional. The number of the occurrence to key off of. Defaults to 1.
* @return {string=}
* If the target is found, the substring that precedes will be returned.
* Otherwise undefined is returned.
*/
String.prototype.before = function(target, opt_occurrence) {
var isRegExp = {}.toString.call(target) == '[object RegExp]',
splits = isRegExp ? [] : this.split(target);
if (isRegExp) {
target = target.global
? target
: new RegExp(target.source, 'g' + (target.ignoreCase ? 'i' : '') + (target.multiline ? 'm' : ''));
this.replace(target, function() {
splits.push(arguments[arguments.length - 2]);
});
}
return Math.abs(opt_occurrence = opt_occurrence || 1) > splits.length - !isRegExp
? null
: isRegExp
? this.slice(0, splits.slice(opt_occurrence - (opt_occurrence > 0))[0])
: splits.slice(0, opt_occurrence).join(target);
};
@westc
Copy link
Author

westc commented Jan 17, 2015

Here are some examples:

console.group('Test String.prototype.before()');
console.group('String tests')
console.log('"asdfasdf".before("d", -3) === null:', "asdfasdf".before("d", -3) === null);
console.log('"asdfasdf".before("d", -2) === "as":', "asdfasdf".before("d", -2) === "as");
console.log('"asdfasdf".before("d", -1) === "asdfas":', "asdfasdf".before("d", -1) === "asdfas");
console.log('"asdfasdf".before("d") === "as":', "asdfasdf".before("d") === "as");
console.log('"asdfasdf".before("d", 1) === "as":', "asdfasdf".before("d", 1) === "as");
console.log('"asdfasdf".before("d", 2) === "asdfas":', "asdfasdf".before("d", 2) === "asdfas");
console.log('"asdfasdf".before("d", 3) === null:', "asdfasdf".before("d", 3) === null);
console.groupEnd();
console.group('RegExp tests');
console.log('"asdf".before(/[sD]/i, -3) === null:', "asdf".before(/[sD]/i, -3) === null);
console.log('"asdf".before(/[sD]/i, -2) === "a":', "asdf".before(/[sD]/i, -2) === "a");
console.log('"asdf".before(/[sD]/i, -1) === "as":', "asdf".before(/[sD]/i, -1) === "as");
console.log('"asdf".before(/[sD]/i) === "a":', "asdf".before(/[sD]/i) === "a");
console.log('"asdf".before(/[sD]/i, 1) === "a":', "asdf".before(/[sD]/i, 1) === "a");
console.log('"asdf".before(/[sD]/i, 2) === "as":', "asdf".before(/[sD]/i, 2) === "as");
console.log('"asdf".before(/[sD]/i, 3) === null:', "asdf".before(/[sD]/i, 3) === null);
console.groupEnd();
console.groupEnd();

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