Claude Pache (2013-02-18T19:00:46.000Z)
github at esdiscuss.org (2013-07-12T02:26:27.584Z)
>> Also, it doesn't seem that hard to implement: >> ```js >> 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: ```js 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: ```js 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: ```js RegExp.escape = function(string) { return string.replace(/([(){}\[\].+*?|\\])/g, '\\$1') } ``` (or: `String.prototype.regExpEscape` ?)