Regex To Match The First Word Of Each Line In A Multiline Text Block

A regular expression to match the first word of each line in a multiline text block (excluding spaces).

/^\w+/gm

Explain:

  • ^ Beginning. Matches the beginning of the string, or the beginning of a line if the multiline flag (m) is enabled.
  • \w Word. Matches any word character (alphanumeric & underscore).
  • + Quantifier. Match 1 or more of the preceding token.
  • g Global.
  • m Multiline.

Matches:

  • This Is A Sentence.
  • This Is A Sentence.
  • This Is A Sentence.

Non-matches:

  • Any spaces found in a string

See Also:

Regex Is Copied!