extensions/html-language-features/server/src/utils/strings.ts/getWordAtText
export function getWordAtText(text: string, offset: number, wordDefinition: RegExp): { start: number; length: number } {
	let lineStart = offset;
	while (lineStart > 0 && !isNewlineCharacter(text.charCodeAt(lineStart - 1))) {
		lineStart--;
	}
	const offsetInLine = offset - lineStart;
	const lineText = text.substr(lineStart);

	// make a copy of the regex as to not keep the state
	const flags = wordDefinition.ignoreCase ? 'gi' : 'g';
	wordDefinition = new RegExp(wordDefinition.source, flags);

	let match = wordDefinition.exec(lineText);
	while (match && match.index + match[0].length < offsetInLine) {
		match = wordDefinition.exec(lineText);
	}
	if (match && match.index <= offsetInLine) {
		return { start: match.index + lineStart, length: match[0].length };
	}

	return { start: offset, length: 0 };
}