我有以下情况。
if (referrer.indexOf("Ral") == -1) { ... }
我想做的是让Ral
不区分大小写,这样它可以是RAl
、rAl
等,仍然可以匹配。
有没有办法说Ral
必须是不区分大小写的?
另一种选择是使用如下的搜索方法。
if (referrer.search(new RegExp("Ral", "i")) == -1) { ...
它看起来更优雅,然后将整个字符串转换为小写,它可能会更有效率。
<br>.如果使用 "toLowerCase()",代码要在字符串上进行两次转换,一次是在整个字符串上进行转换,可能更有效率。
使用toLowerCase()
,代码有两次通过字符串的机会,一次是在整个字符串上将其转换为小写,另一次是寻找所需的索引.
。
对于RegExp
,代码有一次通过字符串,它寻找匹配所需的索引.<br>。
因此,在长字符串上,我建议使用RegExp
版本(我想,在短字符串上,这种效率来自于创建RegExp
对象)。
这里有几种方法。
如果你想对这个实例进行不区分大小写的检查,可以做如下操作。
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) { ...
从ES2016开始,你也可以使用稍微好一点/简单一点/优雅一点的方法。
if (referrer.includes("Ral")) { ... }
或
if (referrer.toLowerCase().includes(someString.toLowerCase())) { ... }
下面是.indexOf()
和.includes()
的一些比较。
https://dev.to/adroitcoder/includes-vs-indexof-in-javascript
现在是2016年,还没有明确的方法? 我希望有一些抄袭的方法。 我'就去试试吧。
设计说明。 我想最大限度地减少内存的使用,从而提高速度--所以没有复制/变换字符串的现象。 我认为V8(和其他引擎)可以优化这个功能。
//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;
}
我之所以取这个名字。
为什么不呢...:
toLowerCase()
- 可能会在同一个字符串上重复调用toLowerCase。RegExp
--用变量搜索很笨拙。
即使是RegExp对象也要转义字符,很不方便。为了更好地进行搜索,请使用以下代码。
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"。
参考文献。 http://freewebdesigntutorials.com/javaScriptTutorials/jsStringObject/indexOfMethod.htm
这是我的看法。
脚本。
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][1]