我需要将字符串中的每个字母加倍
abc -> aabbcc
我试试这个
var s = "abc";
for(var i = 0; i < s.length ; i++){
console.log(s+s);
}
对/对
> abcabc
> abcabc
> abcabc
但我需要
aabbcc
帮助我
最佳答案
使用String#split
, Array#map
和 Array#join
方法。
var s = "abc";
console.log(
// split the string into individual char array
s.split('').map(function(v) {
// iterate and update
return v + v;
// join the updated array
}).join('')
)
更新:您甚至可以使用 String#replace
方法。
var s = "abc";
console.log(
// replace each charcter with repetition of it
// inside substituting string you can use $& for getting matched char
s.replace(/./g, '$&$&')
)
关于javascript - 在js中将字符串中的每个字母加倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40358037/