Special characters

There are characters which have special use in regexps: [ \ ^ $ . | ? * + ( ).

They are special, because they are used to enhance regexp searching abilities. Don’t try to remember the list. You will find them easy to remember after we cover them.

To use a special character as a regular symbol, it must be escaped. Or, in other words, prepended with a backslash.

For example, we need to find the dot '.'. In a regexp, it is a special symbol meaning any character excepts a newline.

So we need to escape it:

showMatch( "Chapter 5.1", /\d\.\d/ )  // 5.1

Without escaping, \d.\d would match 5+1 as well:

showMatch( "5+1 = 6", /\d.\d/ )  // 5+1

Round brackets are also special, so to find an opening bracket, use \(. The example below looks for a worldly character followed by an opening bracket:

showMatch( "function g()", /\w\(/ )  // g(

The slash '/' is not a special in regexps syntax, but in a slashed /...pattern.../ it should be also escaped and inserted as '\/', so the JavaScript parser knows you want the character '/', not finishing the regexp.

Here’s how it looks:

showMatch( "/", /\// )  // '/'



Donate to this project