admin管理员组文章数量:1405870
In my application I have status of an event. I have created an object for those status while writing test scripts like this:
var satus = {
open : 'Open',
inProgress : 'In-Progress'
closed : 'Closed'
};
I have that status displayed in many pages of the application and at different pages it is written in Upper Case letters while not in others.
Can we expect Strings as insensitive?
In my application I have status of an event. I have created an object for those status while writing test scripts like this:
var satus = {
open : 'Open',
inProgress : 'In-Progress'
closed : 'Closed'
};
I have that status displayed in many pages of the application and at different pages it is written in Upper Case letters while not in others.
Can we expect Strings as insensitive?
Share edited Jul 17, 2014 at 5:05 Yi Zeng 32.9k13 gold badges99 silver badges128 bronze badges asked Jul 17, 2014 at 4:51 mohitmohit 7794 gold badges9 silver badges16 bronze badges 2- are u referring asking about Javascript? if so, it is case sensitive – sun_dare Commented Jul 17, 2014 at 4:59
- do you want the strings do be all in lowercase or uppercase? or just the original way the strings are? – misthacoder Commented Jul 17, 2014 at 5:00
3 Answers
Reset to default 1In JavaScript, string parison is case sensitive.
You can pare two strings case insensitively with the following code.
var equal = str1.toUpperCase() === str2.toUpperCase();
Use ReGex with the i
flag to make it case insensitive.
Depending on your use-case, it may be convenient to use a RegEx matcher with the i
flag to make it case-insensitive, like so:
'Test'.match(/test/) //=> null
'Test'.match(/test/i) //=> ["Test", index: 0, input: "Test", groups: undefined]
This works especially well when wanting to do partial matches:
'Test'.match(/te/) //=> null
'Test'.match(/te/i) //=> ["Te", index: 0, input: "Test", groups: undefined]
Do you want like this?
for(var key in status) {
var value = status[key].toLowerCase();
console.log(value);
}
本文标签: javascriptMatching Strings to be case insensitiveStack Overflow
版权声明:本文标题:javascript - Matching Strings to be case insensitive - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744951769a2634126.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论