ice.fo.utils
Version:
29 lines (26 loc) • 898 B
JavaScript
/**
* Find and return list of string that enclosed between [left] and [right] characters
*
* Ex: getStringBetweenCharacters('The {brown} fox jump over the {box}.', '{', '}')
*
* => [brown, box]
*
* @param {string} text
* @param {string} left
* @param {string} right
* @returns
*/
export default function getStringBetweenCharacters(text, left, right) {
// const suffix = right.slice( -1 );
// const regex = new RegExp( `(?<=${ left }).+?(?=${ right }(?!${ suffix }))`, 'g' );
// const matches = text.match( regex );
//
// return matches;
// For IE support
const leftIndex = text.indexOf(left);
if (leftIndex == -1) { return []; }
const rightIndex = text.indexOf(right);
if (rightIndex == -1) { return []; }
return [text.substring(leftIndex + left.length, rightIndex)]
.concat(getStringBetweenCharacters(text.substr(rightIndex + right.length), left, right));
}