Skip to content

Commit

Permalink
[api] Added utile.escapeRegExp
Browse files Browse the repository at this point in the history
  • Loading branch information
pksunkara committed Jun 26, 2012
1 parent 4aed185 commit 5b03ef0
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/index.js
Expand Up @@ -254,6 +254,15 @@ utile.mixin = function (target) {
//
utile.capitalize = utile.inflect.camelize;

//
// ### function escapeRegExp (str)
// #### @str {string} String to be escaped
// Escape string for use in Javascript regex
//
utile.escapeRegExp = function (str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
};

//
// ### function randomString (length)
// #### @length {integer} The number of bits for the random base64 string returned to contain
Expand Down

3 comments on commit 5b03ef0

@jfhbrook
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there docs for what this does somewhere? I don't speak fluent regexp.

@dscape
Copy link

@dscape dscape commented on 5b03ef0 Jun 26, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaces symbols - [ ] / { } ( ) * + ? . \ ^ $ | with their escaped counterpart by pre-pending \\

Why is this useful is perhaps a better question :)

@pksunkara
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add the docs soon. See #12.

Please sign in to comment.