字符串中单词倒序的方法

来源:互联网 发布:2017淘宝双十一成交额 编辑:程序博客网 时间:2024/04/26 04:52

直接上例子吧,太拗口了

"hello world ni hao";  == 》‘hao ni world hello’

方法1 

这个字符串倒序,然后每个单词倒序

</pre><pre>

var s = "hello world ni hao";String.prototype.reverse = function(){return this.split('').reverse().join('');};var res = s.reverse().replace(/(\w+)/g,function(ele){return ele.reverse();});console.log(res);

2,方法二

将每个单词取出成一个数组,倒序充,join即可

var s = "hello world ni hao";var tmp = s.match(/(\w+)/g);var res = tmp.reverse().join(" ");console.log(res);





0 0
原创粉丝点击