Optionally ignoring case in String.prototype.contains()?

# Axel Rauschmayer (12 years ago)

Suggestion: a named parameter ignoreCase:

$ 'hello world'.contains('WORLD')
false

$ 'hello world'.contains('WORLD', { ignoreCase: true })
true
# Kris Kowal (12 years ago)

Perhaps the second argument of contains, startsWith, and endsWith should be consistent with the second argument of the RegExp constructor.

# Norbert Lindenberg (12 years ago)
'Hallo Friedrichstraße'.contains('STRASSE', { ignoreCase: true })

true? false?

'hello İzmir'.contains('İZMİR', { ignoreCase: true })

true? false?

In other words, should ignoreCase be based on Unicode upper case conversion, lower case conversion, or case folding, with or without locale dependent rules? I suspect any choice would surprise some people.

# Tab Atkins Jr. (12 years ago)

On Wed, Aug 28, 2013 at 10:45 AM, Norbert Lindenberg <ecmascript at lindenbergsoftware.com> wrote:

In other words, should ignoreCase be based on Unicode upper case conversion, lower case conversion, or case folding, with or without locale dependent rules? I suspect any choice would surprise some people.

It should be based on exactly the same thing as the regex flag. That is, these two lines should return identical results in all cases:

'Hallo Friedrichstraße'.contains('STRASSE', { ignoreCase: true })
'Hallo Friedrichstraße'.search(/STRASSE/i) != -1

Anything else would be surprising on a language level.

# Domenic Denicola (12 years ago)

More to Norbert's point though, it should probably use the iu flag, not just the i flag.

(Also, you'd need to use the RegExp.escape that people keep talking about, most recently in the RegExp.quoteText thread. But that's minor.)

# Rick Waldron (12 years ago)

This would create inconsistency with indexOf and lastIndexOf, which already have a specified second argument. It's really not painful to make the two strings the same case in user code if ignoring case is desired.