Optionally ignoring case in String.prototype.contains()?
Perhaps the second argument of contains
, startsWith
, and endsWith
should be consistent with the second argument of the RegExp
constructor.
'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.
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.
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.)
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.
Suggestion: a named parameter
ignoreCase
: