常用笔记: JS实现VBS当中的Replace

时间:2022-12-28 10:15:10
<script>
//JS实现VBS当中的Replace,替换全部.方法名大写,区别于原方法,与VBS相类似.
String.prototype.Replace = function(oldValue,newValue,isIgnore)
{
var reg = new RegExp(oldValue,(isIgnore ? 'i' : '') + "g");
return this.replace(reg, newValue);
}
</script>

调用方式:

<script>
var str1 = 'ABCABcAbcaBCaBcabCabc'.Replace('abc','ABC'); // ABCABcAbcaBCaBcabCABC
var str2 = 'ABCABcAbcaBCaBcabCabc'.Replace('abc','ABC',true); // ABCABCABCABCABCABCABC
</script>