DEV Community

Discussion on: 1 line of code: How to reverse all words of a string

Collapse
 
lexlohr profile image
Alex Lohr

This would put the punctuation at the start of the last word, so let me suggest an improved version:

const reverseWords = (str) => str.replace(/\w+/g, (word) => [...word].reverse().join(''))
Enter fullscreen mode Exit fullscreen mode
Collapse
 
martinkr profile image
Martin Krause

Hi Alex,

thank you for taking the time to improve the code.
I like your Idea and updated the article.

Cheers!