直接上例子吧,太拗口了
"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);