// 将 str 中的 a 替换为 A
var str = 'abcdefg';
var result = str.replace('c', 'n');
console.log('result:' + result);
// 输出 result:abndefg
扩展资料:
// 将str 中所有的 a 替换为 A
var str = 'abcabcabc';
var result = str.replace(/a/g, 'A');
console.log('result:' + result);
// 输出 result:AbcAbcAbc
最简单的办法是用数组去替换指定位置的字符。
下面是代码的简单实现,仅供参考:
var a = 'asdfsdfsdfsadf';