Created
January 19, 2019 11:05
-
-
Save ayuLiao/c50519c88617d6e53c51ca43c1cbb688 to your computer and use it in GitHub Desktop.
想将变量的值与字符串中的某些值进行替换
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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