Javascript 不区分大小写全部替换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7313395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:45:20  来源:igfitidea点击:

Case insensitive replace all

javascript

提问by Sergey Metlov

I am looking for any implementation of case insensitive replacing function. For example, it should work like this:

我正在寻找不区分大小写的替换功能的任何实现。例如,它应该像这样工作:

'This iS IIS'.replaceAll('is', 'as');

and result should be:

结果应该是:

'Thas as Ias'

Any ideas?

有任何想法吗?

UPDATE:

更新:

It would be great to use it with variable:

将它与变量一起使用会很棒:

var searchStr = 'is';
'This iS IIS'.replaceAll(searchStr, 'as');

回答by Chandu

Try regex:

尝试正则表达式:

'This iS IIS'.replace(/is/ig, 'as');

Working Example: http://jsfiddle.net/9xAse/

工作示例:http: //jsfiddle.net/9xAse/

e.g:
Using RegExp object:

例如:
使用 RegExp 对象:

var searchMask = "is";
var regEx = new RegExp(searchMask, "ig");
var replaceMask = "as";

var result = 'This iS IIS'.replace(regEx, replaceMask);

回答by Ben Fleming

String.prototype.replaceAll = function(strReplace, strWith) {
    // See http://stackoverflow.com/a/3561711/556609
    var esc = strReplace.replace(/[-\/\^$*+?.()|[\]{}]/g, '\$&');
    var reg = new RegExp(esc, 'ig');
    return this.replace(reg, strWith);
};

This implements exactly the example you provided.

这完全实现了您提供的示例。

'This iS IIS'.replaceAll('is', 'as');

Returns

退货

'Thas as Ias'

回答by Paul

When you use the regex solution you can get problems if your replace string contain e.g. "?". So you have to escape all regex characters or use e.g.:

当您使用正则表达式解决方案时,如果您的替换字符串包含例如“?”,您可能会遇到问题。所以你必须转义所有正则表达式字符或使用例如:

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);
};

this will not change all the occurrences of 'is' in the string. Therefore you can write a while loop in the function.

这不会改变字符串中所有出现的“is”。因此,您可以在函数中编写一个 while 循环。

回答by StefansArya

This is the improvisation from Paul's answer, and there are a performance gap between Regex vs Non-regex

这是 Paul 的即兴回答,Regex 与 Non-regex 之间存在性能差距

The regex code for comparation is taken Benjamin Fleming's answer..

比较的正则表达式代码采用本杰明弗莱明的回答..

JSPerf
Case-sensitive
Regex: 66,542 Operations/sec
Non-Regex: 178,636 Operations/sec (split - join)

JSPerf
区分大小写的正则
表达式:66,542 次操作/秒
非正则表达式:178,636 次操作/秒(拆分 - 连接)

Incase-sensitive
Regex: 37,837 Operations/sec
Non-Regex: 12,566 Operations/sec (indexOf - substr)

区分大小写的正则
表达式:37,837 次操作/秒
非正则表达式:12,566 次操作/秒(indexOf - substr)

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));

回答by Daniel A. White

Use a regular expression.

使用正则表达式。

'This iS IIS'.replace(/is/ig, 'as')

回答by andreszs

I recommend the str_ireplacefunction from php.js, which even replaces strings from arrays.

我推荐php.js 中的 str_ireplace函数,它甚至可以替换数组中的字符串。