Extracts text from between two substrings in a string.
Here's a simple text extraction example:
const text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';textBetween(text, 'ipsum', 'consectetur');// => { ok: true, value: 'dolor sit amet,' } Copy
const text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';textBetween(text, 'ipsum', 'consectetur');// => { ok: true, value: 'dolor sit amet,' }
Here are some examples where the delimiters are special chars to escape:
textBetween('[music=zap]', '[', ']')// => { ok: true, value: 'music=zap' }textBetween('^music=zap.', '^', '.')// => { ok: true, value: 'music=zap' }textBetween('music&playlist?kc|zap', '?', '|')// => { ok: true, value: 'kc' } Copy
textBetween('[music=zap]', '[', ']')// => { ok: true, value: 'music=zap' }textBetween('^music=zap.', '^', '.')// => { ok: true, value: 'music=zap' }textBetween('music&playlist?kc|zap', '?', '|')// => { ok: true, value: 'kc' }
Handle an invalid input string:
textBetween(42, '[', ']') // => { ok: false, error: Error('[strings.textBetween] Expected string value as input') } Copy
textBetween(42, '[', ']') // => { ok: false, error: Error('[strings.textBetween] Expected string value as input') }
string - The starting substring.
string - The ending substring.
A Result object with either the extracted text on success, or an Error object if the provided input is not a string.
Result
Error
Generated using TypeDoc v0.24.7
Extracts text from between two substrings in a string.
Example
Here's a simple text extraction example:
Example
Here are some examples where the delimiters are special chars to escape:
Example
Handle an invalid input string: