Ignoring case in regexes in JavaScript

The i modifier can be used to ignore case sensitivity. Let's see how it's done.

Example

In the following example, the regex will only find small letters:

javascriptlet str = 'aaa bbb CCC DDD';
let res = str.replace(/[a-z]+/g, '!');

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

javascript'! ! CCC DDD'

Example

And now add the i modifier and the regex will start looking for characters in all registers:

javascriptlet str = 'aaa AAA bbb BBB';
let res = str.replace(/[a-z]+/ig, '!');

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

javascript'! ! ! !'

Practical tasks

Задание 1

Simplify your code by using the learned modifier:

javascriptlet res = str.replace(/[a-zA-Z]/g, '!');