Design & Download
Your JS Library
in Seconds!!!

Snippet: Substrings Around A Target

Written by Christopher West (cwest) on August 10, 2016.
Find the substrings before and after a specific target.
'around before after'.replace(/\w+/g, function(name, i) {
  YourJS[name] = function(subject, target, opt_occurrence) {
    var ret, args, indices = [];
    opt_occurrence = parseInt(opt_occurrence || 1, 10);
    target = (typeOf(target, 'RegExp') ? flagRegExp : quoteRegExp)(target, 'g');
    subject.replace(target, function(match, i) {
      args = arguments;
      indices.push([i = args[args.length - 2], match.length + i]);
    });
    ret = (indices = indices[opt_occurrence + (opt_occurrence > 0 ? -1 : indices.length)])
      ? [subject.slice(0, indices[0]), subject.slice(indices[1])]
      : [null, null];
    return i ? ret[i / 7 - 1] : ret;
  };
});

around(...) API Documentation

Description

Finds the substrings around a specific target.

Parameters

  1. subject {string}:
    The string to search.
  2. target {string|RegExp}:
    The target to find and key off of within subject.
  3. opt_occurrence {number}:
    Optional. Defaults to 1. The occurrence of target that you want to key off of within subject. If negative, the occurrence will be counted from the end of subject.

Returns

Returns an array with two values: the string found before target and the string found after target. If target was not found or the specified occurrence (opt_occurrence) of target was not found, [null, null] will be returned.

Example

console.log(YourJS.around('a,b,c,d', ','));       // -> ['a', 'b,c,d']
console.log(YourJS.around('a,b,c,d', ',', 2));    // -> ['a,b', 'c,d']
console.log(YourJS.around('a,b,c,d', ',', 3));    // -> ['a,b,c', 'd']
console.log(YourJS.around('a,b,c,d', ',', 4));    // -> [null, null]
console.log(YourJS.around('a,b,c,d', ',', -1));   // -> ['a,b,c', 'd']
console.log(YourJS.around('a,b,c,d', /^|,/, -1)); // -> ['a,b,c', 'd']
console.log(YourJS.around('a,b,c,d', /^|,/, -4)); // -> ['', 'a,b,c,d']
console.log(YourJS.around('a,b,c,d', /^|,/, -5)); // -> [null, null]

before(...) API Documentation

Description

Finds the substring before a specific target.

Parameters

  1. subject {string}:
    The string to search.
  2. target {string|RegExp}:
    The target to find and key off of within subject.
  3. opt_occurrence {number}:
    Optional. Defaults to 1. The occurrence of target that you want to key off of within subject. If negative, the occurrence will be counted from the end of subject.

Returns

Returns the string found before target. If target was not found or the specified occurrence (opt_occurrence) of target was not found, null will be returned.

Example

console.log(YourJS.before('a,b,c,d', ','));       // -> 'a'
console.log(YourJS.before('a,b,c,d', ',', 2));    // -> 'a,b'
console.log(YourJS.before('a,b,c,d', ',', 3));    // -> 'a,b,c'
console.log(YourJS.before('a,b,c,d', ',', 4));    // -> null
console.log(YourJS.before('a,b,c,d', ',', -1));   // -> 'a,b,c'
console.log(YourJS.before('a,b,c,d', /^|,/, -1)); // -> 'a,b,c'
console.log(YourJS.before('a,b,c,d', /^|,/, -4)); // -> ''
console.log(YourJS.before('a,b,c,d', /^|,/, -5)); // -> null

after(...) API Documentation

Description

Finds the substring after a specific target.

Parameters

  1. subject {string}:
    The string to search.
  2. target {string|RegExp}:
    The target to find and key off of within subject.
  3. opt_occurrence {number}:
    Optional. Defaults to 1. The occurrence of target that you want to key off of within subject. If negative, the occurrence will be counted from the end of subject.

Returns

Returns the string found after target. If target was not found or the specified occurrence (opt_occurrence) of target was not found, null will be returned.

Example

console.log(YourJS.after('a,b,c,d', ','));       // -> 'b,c,d'
console.log(YourJS.after('a,b,c,d', ',', 2));    // -> 'c,d'
console.log(YourJS.after('a,b,c,d', ',', 3));    // -> 'd'
console.log(YourJS.after('a,b,c,d', ',', 4));    // -> null
console.log(YourJS.after('a,b,c,d', ',', -1));   // -> 'd'
console.log(YourJS.after('a,b,c,d', /^|,/, -1)); // -> 'd'
console.log(YourJS.after('a,b,c,d', /^|,/, -4)); // -> 'a,b,c,d'
console.log(YourJS.after('a,b,c,d', /^|,/, -5)); // -> null