Reputation: 2148
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:
"\nabc"
"a\nb"
Does not work:
"\n"
"abc\n"
Upvotes: 0
Views: 71
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