本文介绍了包含不区分大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下内容:
if (referrer.indexOf("Ral") == -1) { ... }
我喜欢做的是赚 Ral
不区分大小写,因此可以 RAl
, rAl
等。并且仍然匹配。
What I like to do is to make Ral
case insensitive, so that it can be RAl
, rAl
, etc. and still match.
有没有办法说 Ral
必须不区分大小写?
Is there a way to say that Ral
has to be case-insensitive?
推荐答案
在之后添加
。此方法将字符串转换为小写字符串。然后,使用 .toLowerCase()
引荐 .indexOf()
使用 ral
而不是 Ral
。
if (referrer.toLowerCase().indexOf("ral") === -1) {
使用正则表达式也可以实现同样的效果(当您想要针对动态模式进行测试时尤其有用): / p>
The same can also be achieved using a Regular Expression (especially useful when you want to test against dynamic patterns):
if (!/Ral/i.test(referrer)) {
// ^i = Ignore case flag for RegExp
这篇关于包含不区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!