method

excerpt

excerpt(text, phrase, options = {})
public

Extracts the first occurrence of phrase plus surrounding text from text. An omission marker is prepended / appended if the start / end of the result does not coincide with the start / end of text. The result is always stripped in any case. Returns nil if phrase isn’t found.

Options

:radius

The number of characters (or tokens — see :separator option) around phrase to include in the result. Defaults to 100.

:omission

The marker to prepend / append when the start / end of the excerpt does not coincide with the start / end of text. Defaults to "...".

:separator

The separator between tokens to count for :radius. Defaults to "", which treats each character as a token.

Examples

excerpt('This is an example', 'an', radius: 5)
# => "...s is an exam..."

excerpt('This is an example', 'is', radius: 5)
# => "This is a..."

excerpt('This is an example', 'is')
# => "This is an example"

excerpt('This next thing is an example', 'ex', radius: 2)
# => "...next..."

excerpt('This is also an example', 'an', radius: 8, omission: '<chop> ')
# => "<chop> is also an example"

excerpt('This is a very beautiful morning', 'very', separator: ' ', radius: 1)
# => "...a very beautiful..."