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
Add a ment  | 

3 Answers 3

Reset to default 1

In 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