I"m using Mean.io and saw an regex in a modRewrite function:
app.use(modRewrite([
'!^/api/.*|\\_getModules|\\.html|\\.js|\\.css|\\.mp4|\\.swf|\\.jp(e?)g|\\.png|\\.gif|\\.svg|\\.ico|\\.eot|\\.ttf|\\.woff|\\.pdf$ / [L]'
]));
I understand that they're trying to rewrite the url to be prettier by replacing any urls containing:
/api/, _getModules, .html, .js, ..., .pdf
However, I have been searching in order to understand the regex but still can't figure it out what is the !^
at the beginning of the line and $
at the end of the line. Could someone please extract the regex step by step?
If an exclamation mark (!) occurs as the first character in a regular expression, all magic characters are treated as special characters. An exclamation mark is treated as a regular character if it occurs anywhere but at the very start of the regular expression.
Special Regex Characters: These characters have special meaning in regex (to be discussed below): . , + , * , ? , ^ , $ , ( , ) , [ , ] , { , } , | , \ . Escape Sequences (\char): To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ).
The $ number language element includes the last substring matched by the number capturing group in the replacement string, where number is the index of the capturing group. For example, the replacement pattern $1 indicates that the matched substring is to be replaced by the first captured group.
*$ means - match, from beginning to end, any character that appears zero or more times. Basically, that means - match everything from start to end of the string.
According to Apache mod_rewrite Introduction:
In mod_rewrite the
!
character can be used before a regular expression to negate it. This is, a string will be considered to have matched only if it does not match the rest of the expression.
The ^
and $
are regex anchors that assert the position at the start and end of string respectively.
To understand the rest, you can go through the What does the regex mean post.
The regex itself is:
^
- Assert the start of string position and.../api/.*
- Match literally /api/
and 0 or more characters other then newline|
- Or... \\_getModules
- Match _getModules
|
- Or\\.html
- Match .html
|\\.js|\\.css|\\.mp4|\\.swf|\\.jp(e?)g|\\.png|\\.gif|\\.svg|\\.ico|\\.eot|\\.ttf|\\.woff|
- Or these extensions (note it will match jpg
and jpeg
as ?
means match 0 or 1 occurrence of preceding pattern)\\.pdf$
- Match .pdf
right at the end of the string ($
).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With