This commit is contained in:
visionmedia 2009-06-30 14:43:40 -07:00
parent 9bc091d339
commit ce22cb78af

View File

@ -157,6 +157,21 @@
return path.replace(/^(\s|\/)*|(\s|\/)*$/g, '')
},
/**
* Normalize _path_. When a RegExp it is simply returned,
* otherwise a string is converted to a regular expression
* surrounded by ^$. So /user/:id/edit would become:
*
* \^/user/(.*?)\/edit\$/
*
* Each param key (:id) will be captured and placed in the
* params array, so param('id') would give the string captured.
*
* @param {string} path
* @return {regexp}
* @api public
*/
pathToRegexp : function(path) {
if (path.constructor == RegExp) return path
Express.regexpKeys = []
@ -164,9 +179,22 @@
Express.regexpKeys.push(key)
return '(.*?)'
})
return new RegExp('^' + this.escapeRegexp(path, '/ [ ]') + '$')
return new RegExp('^' + this.escapeRegexp(path, '/ [ ]') + '$', 'i')
},
/**
* Escape RegExp _chars_ in _string_. Where _chars_
* defaults to regular expression special characters.
*
* _chars_ should be a space delimited list of characters,
* for example '[ ] ( )'.
*
* @param {string} string
* @param {string} chars
* @return {Type}
* @api public
*/
escapeRegexp : function(string, chars) {
var specials = (chars || '/ . * + ? | ( ) [ ] { } \\').split(' ').join('|\\')
return string.replace(new RegExp('(\\' + specials + ')', 'g'), '\\$1')