Categories
JavaScript Answers

How to Do a Case Insensitive Replace All with a JavaScript String?

Spread the love

Sometimes, we want to do a Case insensitive replace all with JavaScript string.

In this article, we’ll look at how to do a Case insensitive replace all with JavaScript string.

Do a Case Insensitive Replace All with a JavaScript String

To do a Case insensitive replace all with JavaScript string, we can use a regex to do the replacement.

For instance, we can write:

const str = 'This iS IIS'.replace(/is/ig, 'as');  
console.log(str)

We call replace on a string with the i and g flags to search for all instances of 'is' in a case insensitive manner.

And we replace each instance with 'as' .

The i flag lets us search for all instances of the pattern in a case-insensitive manner.

The g flag lets us search for all instances of the regex pattern.

As a result, str is 'Thas as Ias’ .

Conclusion

To do a Case insensitive replace all with JavaScript string, we can use a regex to do the replacement.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *