Special characters inside square brackets in JavaScript regex

Special characters inside [ ] become regular characters. This means that they don't need to be escaped with a backslash.

Example

In this example, the search pattern looks like this: any letter 'a', 'b', 'c', or a dot is between x's:

let str = 'xax xbx xcx xdx x.x x@x'; let res = str.replace(/x[abc.]x/g, '!');

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

'! ! ! xdx ! x@x'

Example

In this example, the search pattern looks like this: any small Latin letter or dot is between x's:

let str = 'xax xbx xcx x@x'; let res = str.replace(/x[a-z.]x/g, '!');

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

'! ! ! x@x'

Practical tasks

Given a string:

let str = 'aba aea aca aza axa a.a a+a a*a';

Write a regex that will find the 'a.a', 'a+a', 'a*a' strings without affecting the others.

Given a string:

let str = 'xaz x.z x3z x@z x$z xrz';

Write a regex that matches strings with the pattern: letter 'x', then NOT a dot, NOT an AT sign, and NOT a dollar symbol, and then letter 'z'.

enru