Skip to content

Instantly share code, notes, and snippets.

@kberridge
Created May 11, 2012 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kberridge/2660500 to your computer and use it in GitHub Desktop.
Save kberridge/2660500 to your computer and use it in GitHub Desktop.
razor and javascript regex
@{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