JavaScript

How to Perform Case-Insensitive String Replacement in JavaScript

Sometimes a user may need to replace a string expression with another expression. In such a case, the “replace()” method can be used in JavaScript to perform this operation. Moreover, this method can also perform case-insensitive string replacement. The “Case-insensitive” string replacement means the function will find and match the given string value regardless of whether it is written in upper or lower case and replace it with the desired expression.

This article will provide the procedure for performing the case-insensitive string replacement in JavaScript using the replace() method.

How to Use replace() Method to Perform Case-Insensitive String Replacement in JavaScript?

To understand how the replace() method works in performing the case-insensitive string replacement, let’s evaluate the demonstration given below:

const Str = "I LOVE CODING AND I LOVE JAVASCRIPT";

const editted_Str = Str.replace(/love/gi, "despise");

console.log(editted_Str);

The explanation for the above code is as follows:

  • A dummy string named “Str” is created using the “const” keyword.
  • A constant named “editted_Str” is created that saves the output for the “replace()” method after the replacement:
  • The “replace()” method is used along with the “Str” variable to replace the “love” word with “despise”. This replacement is passed in the “replace()” method as parameters or arguments.
  • The “console.log()” statement will print the resulting string saved in the “editted_Str” constant on the console.

The output of the above-explained code is as follows:

Now, the user may wonder what the alphabet “gi” means in the replace() method used in the above demonstration. These alphabets are tags named “Global” and “Ignore”. Let’s understand these tags through examples.

The “g” (Global) Tag
The global tag makes sure that the given word is replaced as many times as it is present in the original string. If it is not used, only the first instance of the word will be replaced. Let’s try it:

const Str = "I LOVE CODING AND I LOVE JAVASCRIPT";

const editted_Str = Str.replace(/love/i, "despise");

console.log(editted_Str);

In the above code, the “g” tag is not used after the second slash. Therefore, the only first word “love” is replaced with “despise” as shown below:

The “i” (Ignore) Tag
The Ignore tag is used for case insensitive string replacement. Due to the “ignore” tag, the compiler replaces the string content, irrelevant to the case it is written in. Let’s remove the “i” tag from the replace() method and check whether the string is replaced or not:

const Str = "I LOVE CODING AND I LOVE JAVASCRIPT";

const editted_Str = Str.replace(/love/g, "despise");

console.log(editted_Str);

In the above code, the “i” tag is not used after the second slash. The output of that code is as follows:

From the above output, it is clear that the Uppercase word “LOVE” is not replaced because the argument contains the same word but in lowercase.

Note that the strings are immutable in JavaScript. This means that once the string object is created, it cannot be changed. In the above demonstration, the “replace()” method creates a new string that is saved in the constant named “editted_Str”.

That is all for JavaScript replace() method.

Conclusion

The “replace()” method can be used for performing the case-insensitive string replacement. This method takes two arguments. The first argument is the string content that will be replaced, and the second argument is a string that is going to replace the first string. To perform case-insensitive replacement, “ignore” must be used with the first argument in the “replace()” method. This article has provided the procedure for performing case-insensitive string replacement in JavaScript.

About the author

Shaheer Ahmad

I am a Technical Author and Software Engineer with a passion for translating complex concepts into accessible insights. I like to be deeply engaged in the world of technology and craft clear and engaging documentation that provides complex software solutions.