Created
May 11, 2012 15:37
-
-
Save kberridge/2660500 to your computer and use it in GitHub Desktop.
razor and javascript regex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@{var anEmptyRegex = "";} | |
<script type="text/javascript"> | |
var r = /@anEmptyRegex/i; | |
callSomeFunction(r); | |
</script> | |
-- Razor Output -- | |
<script type="text/javascript"> | |
var r = //i; | |
callSomeFunction(r); | |
</script> | |
Do you see what's wrong with that? | |
// is a comment!! | |
-- Fix -- | |
So, if you're dynamically creating a regex like this, and it's at all possible it could be empty, you shouldn't use the regex syntax. Instead, use the object syntax: | |
var r = new RegExp("@anEmptyRegex"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment