Reverse words in array string matching punctuation in Javascript

前端 未结 3 1997
感动是毒
感动是毒 2021-01-22 03:48

How do I reverse the words in this string including the punctuation?

String.prototype.reverse = function () {
    return this.split(\'\').reverse().join(\'\');
         


        
3条回答
  •  北恋
    北恋 (楼主)
    2021-01-22 04:19

    Simply reversing the string wont give the solution.

    1. Get each word.
    2. Reverse It
    3. Again rejoin

    var str = "This is fun, hopefully.";
    alert(str.split("").reverse().join("").split(" ").reverse().join(" "));

提交回复
热议问题