如何反转字符串中的单词而不是反转整个字符串?

How to reverse words in a string instead of reversing the whole string?

function reverseInPlace(str) {
    var words = [];
    words = str.split("\s+");
    var result = "";
    for (var i = 0; i < words.length; i++) {
        return result += words[i].split('').reverse().join('');
    }
}
console.log(reverseInPlace("abd fhe kdj"))

我期望的是 dba ehf jdk,而我得到的是 jdk fhe dba。有什么问题吗?

您需要将字符串拆分为 space

function reverseInPlace(str) {
  var words = [];
  words = str.match(/\S+/g);
  var result = "";
  for (var i = 0; i < words.length; i++) {
     result += words[i].split('').reverse().join('') + " ";
  }
  return result
}
console.log(reverseInPlace("abd fhe kdj"))

Split先把字符串变成单词再反转单个单词

var input = "abd fhe kdj";
var output = input.split( " " ).map(  //split into words and iterate via map
     s => s.split("").reverse().join( "" )  //split individual words into characters and then reverse the array of character and join it back
).join( " " ); //join the individual words

您可以利用splitmap函数来创建反向词。

您需要先使用 space 拆分 sentence,然后您可以将每个单词反转并再次加入反转的单词。

function reverseWord (sentence) {
  return sentence.split(' ').map(function(word) {
    return word.split('').reverse().join('');
  }).join(' ');
}

console.log(reverseWord("abd fhe kdj"));

这个功能应该适合你:

function myFunction(string) {
    return string.split("").reverse().join("").split(" ").reverse().join(" ")
};

如果你看起来像这样 o/p : "Javascript is Best" => "Best is Javascript"

然后可以通过简单的逻辑来完成

//sample string
     let str1 = "Javascript is Best";
//breaking into array
     let str1WordArr = str1.split(" ");
//temp array to hold the reverse string      
      let reverseWord=[];
//can iterate the loop backward      
     for(let i=(str1WordArr.length)-1;i>=0;i--)
     {
//pushing the reverse of words into new array
          reverseWord.push(str1WordArr[i]); 
     }
//join the words array 
      console.log(reverseWord.join(" ")); //Best is Javascript

此解决方案保留白色spaces 个字符space、制表符\t、新行\n 和马车 return \r) 并且也保留它们的顺序(不颠倒):

const sentence = "abd\t fhe kdj";

function reverseWords(sentence) {
    return sentence
        .split(/(\s+)/)
        .map(word => /^\s+$/.test(word) ? word : word.split('').reverse().join(''))
        .join('');
}

console.log(reverseWords(sentence));

If you want to reverse a string by word:
Example: 'Welcome to JavaScript' to 'JavaScript to Welcome'

You can also do something like:
var str = 'Welcome to JavaScript'
function reverseByWord(s){
  return s.split(" ").reverse().join(" ");
}

// Note: There is space in split() and Join() method

reverseByWord(str)
// output will be - JavaScript to Welcome

您可以通过以下代码轻松实现

function reverseWords(str) {
    // Go for it
    let reversed;
    let newArray=[];
    reversed = str.split(" ");
    for(var i = 0;i<reversed.length; i++)
    {
        newArray.push(reversed[i].split("").reverse().join(""));       
    }
    return newArray.join(" ");
}
let reversedString = reverseWords("This is an example!");
console.log("This is the reversed string : ",reversedString);

const reverseWordIntoString = str => str.split(" ").map(word => word.split("").reverse().join('')).join(" ")

const longString = "My name is Vivekanand Panda";
const sentence = "I love to code";


const output = {
  [longString]: reverseWordIntoString(longString),
  [sentence]: reverseWordIntoString(sentence)
}

console.log(output);