Regular Expressions with exclusions and "and" conditions

or how to configure connect-modrewrite

Posted by Andrey Marushkevych on April 4, 2014

In my previous post i mentioned connect-modrewrite middleware, which can be used to enable live reload of angular app. When I was trying to implement regexp for modrewrite, I found it was really challenging to have regular expression with exclusions and “and” conditions.

The solution is to use inverted conditions and then negate the whole expression. Here is the example: Say you need to configure rewrite rule for all request starting with /fr and not containing .js extension:

// starting with /fr and not containing .js

So the way to do it is to create inverted regular expression

// not starting with /fr or containing .js
"^/(?!fr)|\\.js$"

And then negate it in the modrewrite rule:

modRewrite([
    '!^/(?!fr)\\.js$ /fr/index.html [L]'
])


blog comments powered by Disqus