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:

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

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

'! ! CCC DDD'

Example

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

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

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

'! ! CCC DDD'

Practical tasks

Simplify your code by using the learned modifier:

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