added option for trailing non-word characters
(cherry picked from commit 9ba5f40)