Skip to content

Commit

Permalink
Don't escape #, since it has no special meaning in JS regexes.
Browse files Browse the repository at this point in the history
Thanks to Felipe Gasper for catching this.
  • Loading branch information
rgrove committed Nov 24, 2011
1 parent 99c3992 commit 9adc899
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
15 changes: 11 additions & 4 deletions src/escape/HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
Escape Change History
=====================

3.5.0
-----

* `regex()` no longer escapes the `#` character, since it has no special meaning
in JS regexes.


3.4.1
-----

* No changes.
* No changes.


3.4.0
-----

* Non-string arguments to `html()` and `regex()` are now coerced to strings.
[Ticket #2530408]
* Non-string arguments to `html()` and `regex()` are now coerced to strings.
[Ticket #2530408]


3.3.0
-----

* Initial release.
* Initial release.
7 changes: 5 additions & 2 deletions src/escape/js/escape.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Escape = {
characters escaped, allowing the string to be used safely inside a regex.
The following characters, and all whitespace characters, are escaped:
- # $ ^ * ( ) + [ ] { } | \ , . ?
- $ ^ * ( ) + [ ] { } | \ , . ?
If _string_ is not already a string, it will be coerced to a string.
Expand All @@ -60,7 +60,10 @@ Escape = {
@static
**/
regex: function (string) {
return (string + '').replace(/[\-#$\^*()+\[\]{}|\\,.?\s]/g, '\\$&');
// There's no need to escape !, =, and : since they only have meaning
// when they follow a parenthesized ?, as in (?:...), and we already
// escape parens and question marks.
return (string + '').replace(/[\-$\^*()+\[\]{}|\\,.?\s]/g, '\\$&');
},

// -- Protected Static Methods ---------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/escape/tests/functional/escape-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Y.Test.Runner.add(new Y.Test.Case({
},

'regex() should escape regular expression characters': function () {
Assert.areSame('\\-\\#\\$\\^\\*\\(\\)\\+\\[\\]\\{\\}\\|\\\\\\\,\\.\\?\\ \\\t', Escape.regex('-#$^*()+[]{}|\\,.? \t'));
Assert.areSame('\\-#\\$\\^\\*\\(\\)\\+\\[\\]\\{\\}\\|\\\\\\\,\\.\\?\\ \\\t', Escape.regex('-#$^*()+[]{}|\\,.? \t'));
Assert.areSame('\\*\\*\\*', Escape.regex('***'));
Assert.areSame('foo', Escape.regex('foo'));
Assert.areSame('foo\\-bar', Escape.regex('foo-bar'));
Expand Down

0 comments on commit 9adc899

Please sign in to comment.