Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
373 views
in Technique[技术] by (71.8m points)

javascript - 包含不区分大小写的(Contains case insensitive)

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.

(我喜欢做的是让Ral区分大小写的,所以它可以RAlrAl等,仍然匹配。)

Is there a way to say that Ral has to be case-insensitive?

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

  ask by Nate Pet translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add .toLowerCase() after referrer .

(加入.toLowerCase()referrer 。)

This method turns the string in a lower case string.

(此方法将字符串转换为小写字符串。)

Then, use .indexOf() using ral instead of Ral .

(然后,使用ral代替Ral使用.indexOf() 。)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...