Case insensitive String startsWith, contains, endsWith, replaceAll method

David Bruant bruant.d at gmail.com
Sat Feb 16 16:26:54 PST 2013


Le 17/02/2013 00:58, Biju a écrit :
> 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?
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.

> 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.
     "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.

David


More information about the es-discuss mailing list