letreverseWords=(str)=>str.replace(/\S+/g,word=>[...word].reverse().join(""));console.log(reverseWords("The quick brown fox jumps over the lazy dog."));// ehT kciuq nworb xof spmuj revo eht yzal .godconsole.log(reverseWords("double spaced words"));// elbuod decaps sdrowconsole.log(reverseWords("🞀🞁🞂🞃"));// 🞃🞂🞁🞀
I avoided split("") as it breaks with non BMP characters. The spread operator works just as well for splitting strings into individual characters and won't break astral characters.
Notice how if I use split the output is messed up on my last test case.
letreverseWords=(str)=>str.replace(/\S+/g,word=>word.split("").reverse().join(""));console.log(reverseWords("The quick brown fox jumps over the lazy dog."));// ehT kciuq nworb xof spmuj revo eht yzal .godconsole.log(reverseWords("double spaced words"));// elbuod decaps sdrowconsole.log(reverseWords("🞀🞁🞂🞃"));// �🞂🞁🞀�
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Javascript
I avoided split("") as it breaks with non BMP characters. The spread operator works just as well for splitting strings into individual characters and won't break astral characters.
Notice how if I use split the output is messed up on my last test case.