JS反转字符串

例: 'Hello World'

var positive= 'Hello World';
//先用split()将字符串分割为数组,再通过reverse()反转数组,最后用join()将数组转回字符串
var reverse = name.split('').reverse().join(''); 
//'Hello World' -> [H,e,l,l,o, ,W,o,r,l,d] -> [d,l,r,o,W, ,o,l,l,e,H] -> 'dlroW olleH'
console.log(reverse);   //'dlroW olleH '

split() 

var positive= "Hello World";
var reverse = name.split('o') //分割点
console.log(reverse);   //[Hell, W,rld]

join() 

var positive= "Hello World";
//name.split('o').join() //'H,e,l,l,o, ,W,o,r,l,d'
var reverse = name.split('o').join("!") //将数组转换过来的 ',' 替换为 '!'
console.log(reverse);   //'H!e!l!l!o! !W!o!r!l!d'

上一篇:攻防世界-reverse-高手进阶区-elrond32


下一篇:Docker runs WordPress through Nginx reverse proxy to open HTTPS binding domain name