Javascript包含大小写不敏感

我有以下几点:

if (referrer.indexOf("Ral") == -1) { ... } 

我喜欢做的是使Ral不区分大小写,以便它可以是rAlrAl等,并且仍然匹配。

有没有办法说Ral必须不区分大小写?

referrer之后添加.toLowerCase() 。 此方法将string转换为小写string。 然后,使用.indexOf()使用ral而不是Ral

 if (referrer.toLowerCase().indexOf("ral") === -1) { 

使用正则expression式也可以达到同样的效果(当您要针对dynamic模式进行testing时特别有用):

 if (!/Ral/i.test(referrer)) { // ^i = Ignore case flag for RegExp 

另一种select是使用search方法如下:

 if (referrer.search(new RegExp("Ral", "i")) == -1) { ... 

它看起来更优雅,然后将整个string转换为小写,它可能更有效。
使用toLowerCase()代码有两个string传递,一个传递是在整个string将其转换为小写,另一个是寻找所需的索引。
RegExp代码有一个通过它看起来匹配所需的索引的string。

因此,对于长string,我build议使用RegExp版本(我想,在短string这个效率来创buildRegExp对象的帐户虽然)

使用RegExp:

 if (!/ral/i.test(referrer)) { ... } 

或者,使用.toLowerCase()

 if (referrer.toLowerCase().indexOf("ral") == -1) 

这里有几个方法。

如果您想对此实例执行不区分大小写的检查,请执行以下操作。

 if (referrer.toLowerCase().indexOf("Ral".toLowerCase()) == -1) { ... 

或者,如果您定期执行此检查,则可以向String添加一个新的indexOf()类似方法,但使其不区分大小写。

 String.prototype.indexOfInsensitive = function (s, b) { return this.toLowerCase().indexOf(s.toLowerCase(), b); } // Then invoke it if (referrer.indexOfInsensitive("Ral") == -1) { ... 
 if (referrer.toUpperCase().indexOf("RAL") == -1) { ... 

要做更好的search请使用下面的代码,

 var myFav = "javascript"; var theList = "VB.NET, C#, PHP, Python, JavaScript, and Ruby"; // Check for matches with the plain vanilla indexOf() method: alert( theList.indexOf( myFav ) ); // Now check for matches in lower-cased strings: alert( theList.toLowerCase().indexOf( myFav.toLowerCase() ) ); 

在第一个alert()中,JavaScript返回“-1” – 换句话说,indexOf()没有find匹配:这仅仅是因为第一个string中的“JavaScript”是小写字母,第二个string中的大写字母是正确的。 要使用indexOf()执行不区分大小写的search,可以使两个string都是大写或小写。 这意味着,就像第二个alert()一样,JavaScript只会检查你正在查找的string的出现,忽略大小写。

参考, http://freewebdesigntutorials.com/javaScriptTutorials/jsStringObject/indexOfMethod.htm

这是2016年,如何做到这一点没有明确的方式? 我希望有一些副本。 我会去的

devise笔记:我想尽量减less内存使用,从而提高速度 – 所以没有string的复制/变异。 我认为V8(和其他引擎)可以优化这个function。

 //TODO: Performance testing String.prototype.naturalIndexOf = function(needle) { //TODO: guard conditions here var haystack = this; //You can replace `haystack` for `this` below but I wan't to make the algorithm more readable for the answer var needleIndex = 0; var foundAt = 0; for (var haystackIndex = 0; haystackIndex < haystack.length; haystackIndex++) { var needleCode = needle.charCodeAt(needleIndex); if (needleCode >= 65 && needleCode <= 90) needleCode += 32; //ToLower. I could have made this a function, but hopefully inline is faster and terser var haystackCode = haystack.charCodeAt(haystackIndex); if (haystackCode >= 65 && haystackCode <= 90) haystackCode += 32; //ToLower. I could have made this a function, but hopefully inline is faster and terser //TODO: code to detect unicode characters and fallback to toLowerCase - when > 128? //if (needleCode > 128 || haystackCode > 128) return haystack.toLocaleLowerCase().indexOf(needle.toLocaleLowerCase(); if (haystackCode !== needleCode) { foundAt = haystackIndex; needleIndex = 0; //Start again } else needleIndex++; if (needleIndex == needle.length) return foundAt; } return -1; } 

我的名字的理由:

  • 名字中应该有IndexOf
  • 不要添加后缀 – Of指的是以下参数
  • 不要使用那么长的“caseInsensitive”
  • “自然”是一个很好的select,因为默认的区分大小写的比较首先不是人类自然的。

为什么不…:

  • toLowerCase() – 对同一个string的toLowerCase潜在的重复调用。
  • RegExp – 用variablessearch很难。 即使RegExp对象也不得不转义字符

这是我的意见:

脚本

 var originalText = $("#textContainer").html() $("#search").on('keyup', function () { $("#textContainer").html(originalText) var text = $("#textContainer").html() var val = $("#search").val() if(val=="") return; var matches = text.split(val) for(var i=0;i<matches.length-1;i++) { var ind = matches[i].indexOf(val) var len = val.length matches[i] = matches[i] + "<span class='selected'>" + val + "</span>" } $("#textContainer").html(matches.join("")) 

HTML:

 <input type="text" id="search"> <div id="textContainer"> lorem ipsum is simply dummy text of the printing and typesetting industry. lorem ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of letraset sheets containing lorem ipsum passages, and more recently with desktop publishing software like Aldus pagemaker including versions of lorem ipsum.</div> 

Codepen

    Interesting Posts