Skip to content

Helper functions for usage such as parsing text, added get_text() remove_chars() - done #972

@kensoh

Description

@kensoh

for example -

  • get_text(source_text, left_marker, right_marker, optional_count)

examples of usage:
source_text = "welcome to the home... welcome to my home... welcome to our home"

without optional_count, function returns first occurrence of text in between "welcome and "home":
get_text(source_text, 'welcome', 'home') returns "to the"

without optional_count, function returns n occurrence of text in between "welcome and "home":
get_text(source_text, 'welcome', 'home', 2) returns "to my", which is the 2nd occurrence of text in between "welcome" and "home"

function get_text(source_text, left_marker, right_marker, optional_count) {
	var pos1, pos2 = 0;
	if(optional_count === undefined || optional_count === 1)
   	{
		pos1 = source_text.indexOf(left_marker);
		pos2 = source_text.indexOf(right_marker);
   	} else {
		var i = 1;
		while(i <= optional_count) {
			pos1 = source_text.indexOf(left_marker, pos1+1);
			pos2 = source_text.indexOf(right_marker, pos2+1);
			i++;
		}
	}
	return source_text.slice(pos1 + left_marker.length, pos2).trim();
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions