Skip to content

Instantly share code, notes, and snippets.

@ayuLiao
Created January 19, 2019 11:05
Show Gist options
  • Save ayuLiao/c50519c88617d6e53c51ca43c1cbb688 to your computer and use it in GitHub Desktop.
Save ayuLiao/c50519c88617d6e53c51ca43c1cbb688 to your computer and use it in GitHub Desktop.
想将变量的值与字符串中的某些值进行替换
//string:字符串表达式包含要替代的子字符串。
//reallyDo:被搜索的子字符串。
//replaceWith:用于替换的子字符串。
String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) {
if(!RegExp.prototype.isPrototypeOf(reallyDo)) {
return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi" : "g")), replaceWith);
} else {
return this.replace(reallyDo, replaceWith);
}
}
var string = 'abcdefabcdefabcdef';
console.log(string.replaceAll('b',"0",false));//结果:a0cdefa0cdefa0cdef
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment