Javascript Replace a String case insensitive Example

javascript case insensitive replace example; In this tutorial you will learn how to replace a String case insensitive in JavaScript.

To replace all string to case insensitive in JavaScript we can use String.replace method with Set the ignore flag on the first parameter; let’s see the below example;

Example 1:

Here the String.replace method to replace all occurrences of the string CODING with the string master.

const str = 'CODING CODING driver';
const replaced = str.replace(/coding/gi, 'master');
console.log(replaced); // master master driver

The i and g flags on the regular expression. where the i flag stands for ignore and does a case insensitive search in string and the g flag stands for global and replaces all occurrences of the matched string.

Example 2:

If you only want to the first string to replace with the regular expression then remove the g flag;

const str = 'CODING CODING driver';

const replacedOnce = str.replace(/coding/i, 'master');
console.log(replacedOnce); // master CODING world

Hope these examples help you