# 151. 翻转字符串里的单词

var reverseWords = function(s) {
  s = s.trim();
  if (!s) return s;

  return s
    .replace(/\s+/g, "j6_+")
    .split("j6_+")
    .reverse()
    .join(" ");
};
console.log(reverseWords("the sky  is  blue "));
console.log(reverseWords("  hello world  "));
console.log(reverseWords("a good   example"));
console.log(reverseWords("  Bob    Loves  Alice   "));
console.log(reverseWords("Alice does not even like bob"));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Last Updated: 6/27/2023, 7:40:45 PM