Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

151. Reverse Words in a String #6

Open
Ge-yuan-jun opened this issue May 15, 2017 · 1 comment
Open

151. Reverse Words in a String #6

Ge-yuan-jun opened this issue May 15, 2017 · 1 comment

Comments

@Ge-yuan-jun
Copy link

Given an input string, reverse the string word by word.

For example,
Given s = "the sky is blue",
return "blue is sky the"

@Ge-yuan-jun
Copy link
Author

/**
 * @param {string} str
 * @returns {string}
 */
var reverseWords = function(str) {
    return str.replace(/ +/ig,' ').replace(/^ /,'').split(' ').reverse().join(' ').replace(/ ?/,''); 
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant