JavaScript:如何只反转字符串中的单词

时间:2022-09-13 12:20:00

How would i write a function that takes one argument that is a sentence and returns a new sentence where all words are reversed but kept in the same order as the original sentence?

我该如何写一个函数,它取一个论点,一个句子,返回一个新的句子,所有的词都被颠倒了,但是保持原来的句子的顺序不变?

Test Case:

测试用例:

wordsReverser("This is fun, hopefully."); Would return:

wordsReverser(“这很有趣,希望。”);将返回:

"sihT si nuf, yllufepoh."

“sihT si nuf yllufepoh。”

This is what I have so far but notice that I cant get the period and comma to stay in place. I don't know if this was a typo by the interviewer or what?

到目前为止,这是我所拥有的,但是请注意,我不能让句号和逗号保持不变。我不知道这是不是面试官的错字?

function wordsReverser(str){
  return str.split(' ').
    map(function(item) {    
        return item.split('').reverse().join('');
    }).join(' ');
}

wordsReverser("This is fun, hopefully.");
//Output: 'sihT si ,nuf .yllufepoh'

5 个解决方案

#1


1  

Try this way:

试试这种方法:

function wordsReverser(str){
  return str.replace(/[a-zA-Z]+/gm, function(item) {    
        return item.split('').reverse().join('');
    });
}

wordsReverser("This is fun, hopefully.");
//Output: 'sihT si nuf, yllufepoh.'

How It Works: the replace() function will find each word and pass to the function which will reverse the word (the function returns the reverse word which replaces the word in the string) and all other should remain as that was before.

它的工作原理:replace()函数将找到每个单词并传递给函数,该函数将反转单词(函数返回替换字符串中的单词的反向单词),其他所有单词都应该保持原样。

#2


2  

function wordsReverser(str){
  return str.split(/(\s|,|\.)/).
    map(function(item) {    
        return item.split('').reverse().join('');
    }).join('');
}

wordsReverser("This is fun, hopefully.");
// Output: sihT si nuf, yllufepoh.

Regex to extract whitespace, commas and periods.

正则表达式用于提取空格、逗号和句点。

#3


1  

function wordsReverser(str){
  return str.split(' ').
    map(function(item) {    
        var letters = item.match(/[a-z]*/i);
    return item.replace(letters[0], letters[0].split('').reverse().join(''));
}).join(' ');

}

}

 wordsReverser("This is fun, hopefully.");
//Output: 'sihT si nuf, yllufepoh.'

Most likely not fool proof

很有可能不会欺骗证据

#4


1  

The algorithm would be something like this:

算法是这样的:

placeholder_array = [];
  result = '';
  foreach char in inputstring
    if(char !=',' || char != '.'){
         add char to placeholder_array;
    }
    else{
         result = result + reverse of placeholder_array
         placeholder_array = []
    }

   result = result + reverse of placeholder_array

If it's an interview question, I think they'd like more like an algorithm than exact syntax of the language.

如果是面试问题,我想他们更喜欢的是算法而不是语言的精确语法。

#5


1  

So, from what I see you are just reversing the string, the period and comma are associated with the string. The way I am thinking about making it work is grabbing the Regex values, removing them with their index, then insert when youre done at the original index.

所以,从我看到的你只是在颠倒字符串,句号和逗号与字符串相关联。我想让它工作的方式是获取Regex值,用它们的索引删除它们,然后在您在原始索引上完成之后插入。

Hope this helps

希望这有助于

#1


1  

Try this way:

试试这种方法:

function wordsReverser(str){
  return str.replace(/[a-zA-Z]+/gm, function(item) {    
        return item.split('').reverse().join('');
    });
}

wordsReverser("This is fun, hopefully.");
//Output: 'sihT si nuf, yllufepoh.'

How It Works: the replace() function will find each word and pass to the function which will reverse the word (the function returns the reverse word which replaces the word in the string) and all other should remain as that was before.

它的工作原理:replace()函数将找到每个单词并传递给函数,该函数将反转单词(函数返回替换字符串中的单词的反向单词),其他所有单词都应该保持原样。

#2


2  

function wordsReverser(str){
  return str.split(/(\s|,|\.)/).
    map(function(item) {    
        return item.split('').reverse().join('');
    }).join('');
}

wordsReverser("This is fun, hopefully.");
// Output: sihT si nuf, yllufepoh.

Regex to extract whitespace, commas and periods.

正则表达式用于提取空格、逗号和句点。

#3


1  

function wordsReverser(str){
  return str.split(' ').
    map(function(item) {    
        var letters = item.match(/[a-z]*/i);
    return item.replace(letters[0], letters[0].split('').reverse().join(''));
}).join(' ');

}

}

 wordsReverser("This is fun, hopefully.");
//Output: 'sihT si nuf, yllufepoh.'

Most likely not fool proof

很有可能不会欺骗证据

#4


1  

The algorithm would be something like this:

算法是这样的:

placeholder_array = [];
  result = '';
  foreach char in inputstring
    if(char !=',' || char != '.'){
         add char to placeholder_array;
    }
    else{
         result = result + reverse of placeholder_array
         placeholder_array = []
    }

   result = result + reverse of placeholder_array

If it's an interview question, I think they'd like more like an algorithm than exact syntax of the language.

如果是面试问题,我想他们更喜欢的是算法而不是语言的精确语法。

#5


1  

So, from what I see you are just reversing the string, the period and comma are associated with the string. The way I am thinking about making it work is grabbing the Regex values, removing them with their index, then insert when youre done at the original index.

所以,从我看到的你只是在颠倒字符串,句号和逗号与字符串相关联。我想让它工作的方式是获取Regex值,用它们的索引删除它们,然后在您在原始索引上完成之后插入。

Hope this helps

希望这有助于