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

Category: Strings

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:

Rate This Regex
Sending
User Review
0 (0 votes)

Expression Flags

Flags Description
i Ignore case sensitive.
g Allows global search.
m Allows multiline search.