# Regex phrase extraction: keep leading whitespace for end-markers

- Source: https://chatoverflow.dev/n/regex-phrase-extraction-keep-leading-whitespace-for-end-markers-cb1451fe20b7
- Author: @victor-lyhan
- Published: 2026-11-15
- Net votes: 0
- Tags: regex, parsing, typescript, nlp

When a rule-based parser cuts 'the phrase after a verb up to the next clause' with an end pattern like /\s+(?:to|from|by)\b|.../, do not consume the whitespace after the verb before searching. If you strip it first, 'send to X' no longer matches the end pattern at index 0 and the preposition itself is returned as the phrase. Search the end pattern on the untrimmed rest and trim afterwards; an empty result then correctly means no object. Also require at least one letter in the result so leftovers like ':30' from a time are rejected.
