Case insensitive String startsWith, contains, endsWith, replaceAll method
So can we make the default behavior of new methods case insensitive?
I think all current methods are case-sensitive. If these methods were to be made case-insensitive, someone else would come on the list demanding consistency. Also, it doesn't seem that hard to implement:
String.prototype.startsWithI = function(s){
this.match(new RegExp('^'+s, 'i'));
}
And sometimes, case-sensitive is what you want.
We should have a String.replaceAll method right now web developers are using complex logic to achieve the same.
"aA".replace(/a/ig, 'b'); // 'bb'
I feel the i
and g
flag or regexps aren't that complex. One just
needs to know about them.
Someone else would come on the list demanding consistency.
Here I am, demanding consistency. The default should match the behavior of the language's comparison algorithms.
Sometimes, case-sensitive is what you want.
Arguably most of the time. If I want case-insensitive, I will do the extra work for it.
I feel the "i" and "g" flag or regexps aren't that complex. One just needs to know about them.
+1
I'd go so far as saying that none of these are necessary additions in the first place?conveniences at best.
David Bruant wrote:
It doesn't seem that hard to implement:
String.prototype.startsWithI = function(s){ this.match(new RegExp('^'+s, 'i')); }
you also made the common error any developer make (I am ignoring you have missed the return
keyword) If people at es-discuss can make error, how can you expect an average IT developer get it right.
test this
s="fddfd(ghgg"
new RegExp('^'+s, 'i')
Error!!!
And sometimes, case-sensitive is what you want.
I agree, that is why I mentioned to add matchCase parameter. or have startsWithI, containsI, endsWithI instead
I feel the "i" and "g" flag or regexps aren't that complex. One just needs to know about them.
Again you are missing the point that the first parameter of "replace" at many times have to be a variable with a value which was taken from user input.
We you cant agree on replaceAll can we atleast bring mozilla non-standard flags parameter for String.replace as standard.
You can see a lot of people asking for "replace all occurrences"
Actually, it's not just case that users want to ignore. In many use cases, users search for something "similar" to their search string, and the definition of "similar" can vary substantially. For example, an English speaker typically wants "San Jose" to also match "San José", especially when he doesn't know how to type accented characters. For a French speaker, on the other hand, "ne" and "né" are distinct words. Japanese speakers sometimes want to treat all of "あ", "ぁ", "ア", "ア", and "ァ" as similar, or even better, have "たなか" match "田中" based on pronunciation. And in pretty much all cases you want to apply Unicode normalization.
For use cases where you want to select a subset of a list of strings based on similarity, Collator objects from the ECMAScript Internationalization API can be used, with the usage option set to "search". But for the use cases you're primarily concerned about, new API will be needed. Since this is all language and context sensitive, the ECMAScript Language Specification is probably not the right place to define this API.
Norbert
On 18 February 2013 01:24, Norbert Lindenberg <ecmascript at lindenbergsoftware.com> wrote:
Actually, it's not just case that users want to ignore. In many use cases, users search for something "similar" to their search string, and the definition of "similar" can vary substantially. For example, an English speaker typically wants "San Jose" to also match "San José", especially when he doesn't know how to type accented characters. For a French speaker, on the other hand, "ne" and "né" are distinct words. Japanese speakers sometimes want to treat all of "あ", "ぁ", "ア", "ア", and "ァ" as similar, or even better, have "たなか" match "田中" based on pronunciation. And in pretty much all cases you want to apply Unicode normalization.
Biju: I could accept that argumnet
For use cases where you want to select a subset of a list of strings based on similarity, Collator objects from the ECMAScript Internationalization API can be used, with the usage option set to "search". But for the use cases you're primarily concerned about, new API will be needed. Since this is all language and context sensitive, the ECMAScript Language Specification is probably not the right place to define this API.
Biju: Then let me redefine my request. Instead of "Ignore case", match text by what ever satisfying "i" flag for RegExp defined in ECMA script/the browser/user agent. Dont tell me now there is nothing defined for "i" flag.
Also nothing above is stopping standardization of mozilla non-standard flags parameter for String.replace as standard. developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/replace#Syntax
Also, it doesn't seem that hard to implement:
String.prototype.startsWithI = function(s){ this.match(new RegExp('^'+s, 'i')); }
you also made the common error many developers make
Indeed (and I am surprised that David made such an error). The following implementation is more robust:
String.prototype.startsWithI = function(s) {
this.toLowerCase().startsWith(s.toLowerCase())
}
This approach will work for many, but not all, string functions.
The first parameter of "replace" at many times have to be a variable with a value which was taken from user input.
If there were a RegExp.escape
function that escapes all characters in a string that have significance in a regular expression, you could write:
str.replace(new RegExp(RegExp.escape(searchString), 'ig'), replacement)
But sadly, such a function does not exist in EcmaScript, unless I missed it. Perhaps the following convenience function could be added:
RegExp.escape = function(string) {
return string.replace(/([(){}\[\].+*?|\\])/g, '\\$1')
}
(or: String.prototype.regExpEscape
?)
In most time when user want to search something in a text, he/she wants to do a case insensitive search. For example to filter items displayed in list on a page. Also on other applications, say any word processor, or in page search in Firefox, IE, Chrome etc.
So can we make the default behavior of new methods String.startsWith, String.contains, String.endsWith case insensitive?
And to make it case sensitive we should add a third flag parameter matchCase like...
var startsWith = str.startsWith(searchString [, position [, matchCase] ] ); var contained = str.contains(searchString [, position [, matchCase] ] ); var endsWith = str.endsWith(searchString [, position [, matchCase] ] );
Additionally we should have a String.replaceAll method right now web developers are using complex logic to achieve the same. (Again with insensitive search by default.) String.replace is not helping, as it default to case sensitive and a one time operation, if the first parameter is not a regular expression.
We could also add String.replaceFirst and String.replaceLast method.
If we dont want change behavior of String.startsWith, String.contains, String.endsWith to case insensitive. Can we have another set methods probably named String.startsWithI, String.containsI, String.endsWithI (where "I" stands for ignore/insensitive case)
Cheers Biju
harmony:string_extras