我有以下几点:
if (referrer.indexOf("Ral") == -1) { ... }
我喜欢做的是使Ral不区分大小写,这样它可以是Ral, Ral等,仍然匹配。
有办法说拉尔必须不区分大小写吗?
我有以下几点:
if (referrer.indexOf("Ral") == -1) { ... }
我喜欢做的是使Ral不区分大小写,这样它可以是Ral, Ral等,仍然匹配。
有办法说拉尔必须不区分大小写吗?
当前回答
以下是我的看法:
脚本:
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
其他回答
以下是我的看法:
脚本:
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
这样更好~!
if (~referrer.toUpperCase().indexOf("RAL")) {
console.log("includes")
}
以下是根据ES6按性能降序排列的选项
包括
if (referrer.toLowerCase().includes("Ral".toLowerCase())) { ... }
IndexOf(这有时会给出类似或更好的结果比Includes)
if (referrer.toLowerCase().indexOf("Ral".toLowerCase()) !== -1) { ... }
匹配
if (referrer.match(new RegExp("Ral", 'i'))) { ... }
基准测试结果:https://jsben.ch/IBbnl
if (referrer.toUpperCase().indexOf("RAL") == -1) { ...
使用RegExp:
if (!/ral/i.test(referrer)) {
...
}
或者,使用.toLowerCase():
if (referrer.toLowerCase().indexOf("ral") == -1)