@simongrasinovski/str-utils
Version:
A lightweight utility library for effortless string manipulation. Simplify common tasks like case validation, conversion and formatting with easy-to-use functions that enhance your JavaScript applications
26 lines (24 loc) • 850 B
JavaScript
;
var _require = require('./utils'),
ensureString = _require.ensureString;
/**
* Extracts a substring between two characters.
*
* @function
* @param {string} input - The string to search.
* @param {string} startChar - Start character.
* @param {string} endChar - End character.
* @returns {string} Exctacted substring.
*
* @throws {TypeError} Throws an error if the input is not a string.
*
* @example
* const result = substringBetween('hello world, hello [from] string utils', '[', ']');
* console.log(result); // 'from'
*/
module.exports = ensureString(function (input, from, to) {
var startIndex = input.indexOf(from);
var endIndex = input.indexOf(to, startIndex + from.length);
if (startIndex === -1 || endIndex === -1) return '';
return input.substring(startIndex + from.length, endIndex);
});