Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Don't escape #, since it has no special meaning in JS regexes.

Thanks to Felipe Gasper for catching this.
  • Loading branch information...
commit 9adc89997099d6b58185a60b5288737ab5ed8749 1 parent 99c3992
@rgrove rgrove authored
View
15 src/escape/HISTORY.md
@@ -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.
View
7 src/escape/js/escape.js
@@ -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.
@@ -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 ---------------------------------------------
View
2  src/escape/tests/functional/escape-test.js
@@ -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'));

0 comments on commit 9adc899

Please sign in to comment.
Something went wrong with that request. Please try again.