# Hat symbol inside sets in JavaScript regex

As you know, the hat inside `[ ]` perform a negation when written at the beginning of the brackets. So, it is a special character inside these brackets. To get a hat as a regular character, you need to either escape it or remove it from the first position.

## Example

In the following example, the search pattern is: any first character except `'d'`, then the two letters `'x'`.

`let str = 'axx bxx ^xx dxx'; let res = str.replace(/[^d]xx/g, '!');`

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

`'! ! ! dxx'`

## Example

And now the search pattern is: the first character is `'d'` or `'^'`, then two letters `'x'`:

`let str = 'axx bxx ^xx dxx'; let res = str.replace(/[d^]xx/g, '!');`

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

`'axx bxx ! !'`

## Example

You can not remove the hat from the first position, but just escape it with a backslash, so it will begin to denote itself:

`let str = 'axx bxx ^xx dxx'; let res = str.replace(/[\^d]xx/g, '!');`

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

`'axx bxx ! !'`

## Practical tasks

Given a string:

`let str = '^xx axx ^zz bkk @ss';`

Write a regex that finds strings according to the pattern: hat or AT symbol, and then two Latin letters.

Given a string:

`let str = '^xx axx ^zz bkk @ss';`

Write a regex that finds strings according to the pattern: a NON-hat and non-AT symbol, and then two Latin letters.

Given a string:

`let str = '^xx axx ^zz bkk';`

Write a regex that will find strings according to the pattern: a non-hat and non-space, and then two Latin letters.

← →
↑
en ru