Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the exclamation mark at the beginning and dollar sign at the end of regex?

Tags:

regex

mean.io

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?

like image 577
lvarayut Avatar asked Aug 07 '15 11:08

lvarayut


People also ask

What does exclamation mark do in regex?

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.

What are the regex special characters?

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 ( \ ).

What does $1 do in regex?

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.

What does *$ mean in regex?

*$ 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.


1 Answers

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 ($).
like image 166
Wiktor Stribiżew Avatar answered Oct 20 '22 15:10

Wiktor Stribiżew