我正在寻找一个不区分大小写的替换函数的实现。例如,它应该像这样工作:
'This iS IIS'.replaceAll('is', 'as');
结果应该是:
'Thas as Ias'
任何想法吗?
更新:
最好能够与变量一起使用。var searchStr = 'is';
'This iS IIS'.replaceAll(searchStr, 'as');
我正在寻找一个不区分大小写的替换函数的实现。例如,它应该像这样工作:
'This iS IIS'.replaceAll('is', 'as');
结果应该是:
'Thas as Ias'
任何想法吗?
更新:
最好能够与变量一起使用。var searchStr = 'is';
'This iS IIS'.replaceAll(searchStr, 'as');
尝试使用正则表达式:
'This iS IIS'.replace(/is/ig, 'as');
工作示例:http://jsfiddle.net/9xAse/
例如:
使用RegExp对象:
var searchMask = "is";
var regEx = new RegExp(searchMask, "ig");
var replaceMask = "as";
var result = 'This iS IIS'.replace(regEx, replaceMask);
String.prototype.replaceAll = function(strReplace, strWith) {
// See https://dev59.com/AXA65IYBdhLWcg3w7DT8#3561711
var esc = strReplace.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
var reg = new RegExp(esc, 'ig');
return this.replace(reg, strWith);
};
这个实现完全按照你提供的例子。
'This iS IIS'.replaceAll('is', 'as');
返回
'Thas as Ias'
当您使用正则表达式解决方案时,如果您的替换字符串包含例如"?",那么您可能会遇到问题。因此,您必须转义所有正则表达式字符或者使用例如:
当你使用正则表达式解决方案时,如果你的替换字符串中包含例如"?"字符,那么你可能会遇到问题。所以你需要转义所有正则表达式特殊字符,或者使用例如:
String.replacei = String.prototype.replacei = function (rep, rby) {
var pos = this.toLowerCase().indexOf(rep.toLowerCase());
return pos == -1 ? this : this.substr(0, pos) + rby + this.substr(pos + rep.length);
};
这不会改变字符串中所有“is”的出现次数。因此,您可以在函数中编写while循环。
String.prototype.replaces = function(str, replace, incaseSensitive) {
if(!incaseSensitive){
return this.split(str).join(replace);
} else {
// Replace this part with regex for more performance
var strLower = this.toLowerCase();
var findLower = String(str).toLowerCase();
var strTemp = this.toString();
var pos = strLower.length;
while((pos = strLower.lastIndexOf(findLower, pos)) != -1){
strTemp = strTemp.substr(0, pos) + replace + strTemp.substr(pos + findLower.length);
pos--;
}
return strTemp;
}
};
// Example
var text = "A Quick Dog Jumps Over The Lazy Dog";
console.log(text.replaces("dog", "Cat", true));
str
字符串位于输入字符串的开头,则会导致无限循环。 - EvaristeString.prototype.replaces = function(str, replace, incaseSensitive) {
if(!incaseSensitive){
return this.split(str).join(replace);
} else {
// Replace this part with regex for more performance
var strLower = this.toLowerCase();
var findLower = String(str).toLowerCase();
var strTemp = this.toString();
var pos = strLower.length;
while((pos = strLower.lastIndexOf(findLower, pos)) != -1){
strTemp = strTemp.substr(0, pos) + replace + strTemp.substr(pos + findLower.length);
if (pos == 0) break; // Fixes bug
pos--;
}
return strTemp;
}
};
// Example
var text = "Dog Jumps Over The Lazy Dog";
console.log(text.replaces("dog", "Cat", true));
'This iS IIS'.replace(/is/ig, 'as')
不敏感的ReplaceAll:12 053 033被1秒替换
用ReplaceAll("狗跳过懒惰的狗", "狗", "猫")
例子:
function ReplaceAll(base,told,tnew) { // replace insensitive
if (told=="") {return base;}
var u=base.toLowerCase();
var t=told.toLowerCase();
var ss=""; var p=-1; var i=0;
p=u.indexOf(t);
if (p>-1) {
let mLen=told.length;
while (p>=0) {
ss+=base.substr(0,p)+tnew;
i=p+mLen;
base=base.substr(i);
u=u.substr(i);
p=u.indexOf(t);
}
ss+=base; //fix bug
return ss;
}
return base;
}
/*Test:
function loopThrough() {
for (let i = 0; i < 60000000; i++) {
let s=ReplaceAll("Dog Jumps Over The Lazy Dog","dog","Cat");
}
}
$(document).ready(function(){
$("button").click(function(){
//alert(ReplaceAll("Dog Jumps Over The Lazy Dog","dog", "Cat"));
document.getElementById("demo").innerHTML = '...';
let startTime = new Date();
loopThrough();
let endTime = new Date();
let timeElapsed = endTime - startTime;
document.getElementById("demo").innerHTML = "elapsed times: " + timeElapsed + " milliseconds.";
*/
function replaceCaseInsensitive(str, find, replace) {
let result = "";
let findLower = find.toLowerCase();
let index = 0;
while (index < str.length) {
if (str.substring(index).toLowerCase().startsWith(findLower)) {
result += replace;
index += find.length;
} else {
result += str[index];
index++;
}
}
return result;
}
// Example usage
let originalString = "Hello World";
let newString = replaceCaseInsensitive(originalString, "world", "Universe");
console.log(newString); // Output: Hello Universe
我推荐使用 php.js 中的 str_ireplace 函数,它甚至可以替换数组中的字符串。
.replace(searchMask, replaceMask)
? - Sergey Metlov