如果字符串末尾有 space,如何使正则表达式不匹配

How to make a regex not match if the string has a space at the end

我正在尝试创建一个正则表达式,仅当字符串末尾没有 space 时才匹配该字符串。

例如,这些应该匹配:"Jake" "John" "Mary Anne"

这些不应该匹配:"Don " "David "

这可能吗?我能够编写一个简单的表达式,完全不允许 spaces,但这对于某些输入来说是个问题。

是的,有可能。
试试下面的正则表达式:

^.*\S$

它允许所有不以 space 字符结尾的内容。

DEMO 有解释