包含不区分大小写
我有以下内容:
if (referrer.indexOf("Ral") == -1) { ... }
我喜欢做的是使 Ral
不区分大小写,以便它可以是 RAl
、rAl
等,并且仍然匹配。
有没有办法说 Ral 必须不区分大小写?
I have the following:
if (referrer.indexOf("Ral") == -1) { ... }
What I like to do is to make Ral
case insensitive, so that it can be RAl
, rAl
, etc. and still match.
Is there a way to say that Ral
has to be case-insensitive?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
在
referrer
之后添加.toUpperCase()
。该方法将字符串转换为大写字符串。然后,使用.indexOf()
使用RAL
而不是Ral
。使用正则表达式也可以实现相同的目的(当您想要测试动态模式时特别有用):
Add
.toUpperCase()
afterreferrer
. This method turns the string into an upper case string. Then, use.indexOf()
usingRAL
instead ofRal
.The same can also be achieved using a Regular Expression (especially useful when you want to test against dynamic patterns):
另一种选择是使用搜索方法,如下所示:
它看起来更优雅,然后将整个字符串转换为小写,并且可能更有效。
使用
toLowerCase()
代码对字符串进行两次传递,一次是在整个字符串上将其转换为小写,另一次是查找所需的索引。使用
RegExp
,代码只需遍历一次看起来与所需索引匹配的字符串。因此,对于长字符串,我建议使用
RegExp
版本(我猜测在短字符串上这种效率来自于创建RegExp
对象)Another options is to use the search method as follow:
It looks more elegant then converting the whole string to lower case and it may be more efficient.
With
toLowerCase()
the code have two pass over the string, one pass is on the entire string to convert it to lower case and another is to look for the desired index.With
RegExp
the code have one pass over the string which it looks to match the desired index.Therefore, on long strings I recommend to use the
RegExp
version (I guess that on short strings this efficiency comes on the account of creating theRegExp
object though)从 ES2016 开始,您还可以使用稍微更好/更简单/更优雅的方法(区分大小写):
或(不区分大小写):
以下是
.indexOf()
和.includes( )
:https://dev.to/adroitcoder/includes-vs-indexof-in-javascript
From ES2016 you can also use slightly better / easier / more elegant method (case-sensitive):
or (case-insensitive):
Here is some comparison of
.indexOf()
and.includes()
:https://dev.to/adroitcoder/includes-vs-indexof-in-javascript
使用正则表达式:
或者使用
.toLowerCase()
:Use a RegExp:
Or, use
.toLowerCase()
:这里有几种方法。
如果您只想对此实例执行不区分大小写的检查,请执行如下操作。
或者,如果您定期执行此检查,则可以向
String
添加新的类似indexOf()
的方法,但使其不区分大小写。There are a couple of approaches here.
If you want to perform a case-insensitive check for just this instance, do something like the following.
Alternatively, if you're performing this check regularly, you can add a new
indexOf()
-like method toString
, but make it case insensitive.你可以试试这个
You can try this
以下是按照 ES6 按性能降序排列的选项
包含
IndexOf (这有时会提供与包含相似或更好的结果)
匹配
基准结果: https://jsben.ch/IBbnl
Here are the options as per ES6 in decreasing order of performance
Includes
IndexOf (this sometimes gives similar or better results than Includes)
Match
Benchmark results: https://jsben.ch/IBbnl
任何语言的示例:
Example for any language:
现在已经是 2016 年了,还没有明确的方法来做到这一点吗?我希望能吃到一些复制意大利面。我会去尝试一下。
设计说明:我想最大限度地减少内存使用,从而提高速度 - 所以没有字符串的复制/变异。我认为 V8(和其他引擎)可以优化此功能。
我的名字理由:
为什么不...:
toLowerCase()
- 可能会在同一字符串上重复调用 toLowerCase。RegExp
- 使用变量搜索很尴尬。即使是 RegExp 对象也很尴尬,必须转义字符It's 2016, and there's no clear way of how to do this? I was hoping for some copypasta. I'll have a go.
Design notes: I wanted to minimize memory usage, and therefore improve speed - so there is no copying/mutating of strings. I assume V8 (and other engines) can optimise this function.
My reason for the name:
Why not...:
toLowerCase()
- potential repeated calls to toLowerCase on the same string.RegExp
- awkward to search with variable. Even the RegExp object is awkward having to escape characters为了进行更好的搜索,请使用以下代码,
在第一个alert()中,JavaScript返回“-1” - 换句话说,indexOf()没有找到匹配项:这只是因为“JavaScript”在第一个中是小写的字符串,并在第二个中正确大写。要使用indexOf()执行不区分大小写的搜索,您可以将两个字符串设置为大写或小写。这意味着,就像在第二个alert()中一样,JavaScript只会检查您要查找的字符串是否出现,而忽略大小写。
参考,
http://freewebdesigntutorials.com/javaScriptTutorials/jsStringObject/indexOfMethod.htm
To do a better search use the following code,
In the first alert(), JavaScript returned "-1" - in other words, indexOf() did not find a match: this is simply because "JavaScript" is in lowercase in the first string, and properly capitalized in the second. To perform case-insensitive searches with indexOf(), you can make both strings either uppercase or lowercase. This means that, as in the second alert(), JavaScript will only check for the occurrence of the string you are looking for, capitalization ignored.
Reference,
http://freewebdesigntutorials.com/javaScriptTutorials/jsStringObject/indexOfMethod.htm
如果
referrer
是一个数组,则可以使用findIndex()
If
referrer
is an array, you can usefindIndex()
这是我的看法:
脚本:
HTML:
代码笔
Here's my take:
Script:
HTML:
Codepen
这样就更好了~!
It is better~!