ప్రధాన కంటెంట్‌కు వెళ్లండి
eLearner.app
మాడ్యూల్ 1 · 4లో పాఠం 4కోర్సులో 4/32~10 min
మాడ్యూల్ పాఠాలు (4/4)

మెటా-పాత్రల నుండి తప్పించుకోవడం

Some characters in regex have a special meaning: they are called meta-characters. If you want to match the literal character (e.g. a real ., a real +, a real parenthesis) you need to prefix them with a backslash \, which escapes them.

The meta-characters to remember are twelve:

Code
.  *  +  ?  ^  $  |  \  (  )  [  ]  {  }  /

(The / is not really an engine meta-character, but it must be escaped in the JavaScript literal notation /.../.)

Code
Pattern: \$\d+\.\d\d
Sample:  Prezzi: $9.99, $12.50, totale $22.49.
                 ^^^^^  ^^^^^^         ^^^^^^

Here we escape $ (end-of-string anchor) and . (wildcard) to match the literal characters in the price format "dollar + number + dot + two digits".

Double backslash in JS strings

When you write a pattern inside a JavaScript string (like you do in our exercises) the backslash must be doubled. The JS string '\\d' contains two characters (\ and d) and is exactly the regex pattern \d.

JS
const re1 = /\d+/; // literal notation: a single backslash
const re2 = new RegExp('\\d+'); // string: double backslash

The course editor shows the pattern already decoded (you see a single \d); you do not have to double anything yourself.

The 12 special meta-characters

The characters with special meanings in the engine are exactly 12: \\\ ^ $ . | ? * + ( ) [ {. If you want to match them as literal text, you must precede them with a backslash. In some environments (e.g. non-raw JS strings), you might need to double the backslash (\\\\).

Try it

వ్యాయామం#regex.m1.l4.e1
ప్రయత్నాలు: 0లోడ్ అవుతోంది...

Find every price in the `$N.NN` format (dollar, one or more digits, dot, two digits). Remember to escape `$` and `.`.

ఎడిటర్ లోడ్ అవుతోంది…
సూచనను చూపించు

$ must be escaped as \\$, the dot as \\. - otherwise the dot matches anything.

3 ప్రయత్నాల తర్వాత పరిష్కారం లభిస్తుంది

Review exercise

వ్యాయామం#regex.m1.l4.e2
ప్రయత్నాలు: 0లోడ్ అవుతోంది...

Find every literal question mark `?` in the text. `?` is a meta-character: it must be escaped.

ఎడిటర్ లోడ్ అవుతోంది…
సూచనను చూపించు

Without an escape, ? would be a quantifier (module 2). Put a \\ in front of it.

3 ప్రయత్నాల తర్వాత పరిష్కారం లభిస్తుంది

Additional challenge

వ్యాయామం#regex.m1.l4.e3
ప్రయత్నాలు: 0లోడ్ అవుతోంది...

Find all occurrences of `.tar.gz` in the text, ensuring that the dots are matched literally.

ఎడిటర్ లోడ్ అవుతోంది…
సూచనను చూపించు

Insert a backslash before each dot to escape it.

3 ప్రయత్నాల తర్వాత పరిష్కారం లభిస్తుంది