-
Notifications
You must be signed in to change notification settings - Fork 724
Closed
Labels
Description
This is very useful when extracting text from a chunk of text content, by providing the left anchor and right anchor.
aisingapore/TagUI#972 (comment)
// retrieve text between 2 provided anchors in given text content
function get_text(source_text, left_marker, right_marker, optional_count) {
if (!source_text || !left_marker || !right_marker) return '';
var left_position = source_text.indexOf(left_marker); if (left_position == -1) return '';
var right_position = source_text.indexOf(right_marker, left_position+1); if (right_position == -1) return '';
if (optional_count > 1) {var occurrence_count = 2; while(occurrence_count <= optional_count) {occurrence_count++;
left_position = source_text.indexOf(left_marker, right_position+1); if (left_position == -1) return '';
right_position = source_text.indexOf(right_marker, left_position+1); if (right_position == -1) return '';}}
return source_text.slice(left_position + left_marker.length, right_position).trim();}