var str = "test-test-test";
str = "test-test-test".replace("test","ok");
console.log(str);
;
str = "test-test-test".replace(/test/g,1)">);
console.log(str);
var faces = {
"/::)": "weixiao","/::~": "pizui"
};
var str = "/::)-/::B-/::)-/:8-)-/:8-)";
for (var k in faces) {
while(str.indexOf(k) > -1) {
str = str.replace(k,faces[k]);
}
}
console.log(str);
var str = "/::)-/::B-/:hehe-/:8-)-/:8-)"
var p = -1; // 字符出现位置
var s = 0; 下一次起始位置
while((p = str.indexOf(k,s)) > -1) {
s = p + faces[k].length; 位置 + 值的长度
str =
/**
* 字符串替换
* @param {string} str 要被替换的字符串
* @param {string} substr 要替换的字符串
* @param {string} newstr 用于替换的字符串
* @return {string} 替换后的新字符串
*/
function replace(str,substr,newstr) {
下一次起始位置
while((p = str.indexOf(substr,1)">) {
s
= p + newstr.length; str.replace(substr,newstr);
}
return str;
}
console.log( replace("ssssss","ss","s") ); sss
转义字符串中的元字符
var re = new RegExp(substr,"g"); 生成正则
str.replace(re,newstr);
}
console.log( replace("ssssss",1)"> sss