Escaping special characters in regex in JavaScript

Suppose we want to use a special character literally. To do this, it must be escaped with a backslash. Let's look at examples.

Example

In the following example, the author of a regex wanted the search pattern to look like this: letter 'a', then plus sign '+', then letter 'x'. However, the code author didn't escape the character '+' and so the search pattern actually looks like this: letter 'a' one or more times, then letter 'x':

let str = 'a+x ax aax aaax'; let res = str.replace(/a+x/g, '!');

As a result, the following will be written to the variable:

'a+x ! ! !'

Example

And now the author has escaped the plus sign with a backslash. The search pattern now looks as it should be: letter 'a', then plus '+', then letter 'x'.

let str = 'a+x ax aax aaax'; let res = str.replace(/a\+x/g, '!');

As a result, the following will be written to the variable:

'! ax aax aaax'

Example

In this example, the pattern looks like this: letter 'a', then dot '.', then letter 'x'⁅/r]:

let str = 'a.x abx azx'; let res = str.replace(/a\.x/g, '!');

As a result, the following will be written to the variable:

'! abx azx'

Example

And in the following example, the author forgot to escape the slash and all substrings fell under the regex, since an unescaped dot denotes any character:

let str = 'a.x abx azx'; let res = str.replace(/a.x/g, '!');

As a result, the following will be written to the variable:

'! ! !'

Comment

Please note that if you forget the backslash for a dot (when it should denote itself) - this may not even be noticed:

'a.x'.replace(/a.x/g, '!'); // returns '!', as we wanted

Visually it works correctly (because the dot denotes any character, including the usual dot '.'). But if we change the string in which the replacements occur, we will see our mistake:

'a.x abx azx'.replace(/a.x/g, '!'); // returns '! ! !', but '! abx azx' was expected

List of special and regular characters

If you escape an ordinary character - nothing terrible will happen - it will still denote itself. Exception - numbers, they cannot be escaped.

Often there is doubt whether a given character is special. Some go so far as to escape all suspicious characters in a row. However, this is bad practice (cluttering up the regex with backslashes).

The following are special characters: $ ^ . * + ? \ / {} [] () |

The following aren't special characters: @ : , ' " ; - _ = < > % # ~ `& !

Practical tasks

Given a string:

let str = 'a.a aba aea';

Write a regex that matches the string 'a.a' without capturing the rest.

Given a string:

let str = '2+3 223 2223';

Write a regex that matches the string '2+3' without capturing the rest.

Given a string:

let str = '23 2+3 2++3 2+++3 345 567';

Write a regex that matches the '2+3', '2++3', '2+++3' strings without capturing the rest (there can be any number of +).

Given a string:

let str = '23 2+3 2++3 2+++3 445 677';

Write a regex that matches the '23', '2+3', '2++3', '2+++3' strings without capturing the rest.

Given a string:

let str = '*+ *q+ *qq+ *qqq+ *qqq qqq+';

Write a regex that matches the '*q+', '*qq+', '*qqq+' strings without capturing the rest.

Given a string:

let str = '[abc] {abc} abc (abc) [abc]';

Write a regex that matches strings in square brackets and replaces them with '!'.

enru