User
User

Reputation: 2148

Regex to strip line break at the end of line

Why this regex does not strip "\n" from the end of line, but strips it in the middle? "\n" should be disallowed at any position.

Regex: "^[\w ]*$"

Works:

Does not work:

Upvotes: 0

Views: 71

Answers (1)

Sebastian Proske
Sebastian Proske

Reputation: 8413

Even in non-multiline mode, the $ allows a single trailing linebreak to follow. That is stated in the documentation:

Matches the end of the string or just before the newline at the end of the string

To avoid, you can add a lookahead to check that no newline follow, like

^[\w ]*$(?!\n)

Upvotes: 1

Related Questions