Finding and Replacing Substrings Regardless of Case

var s1 = "jQuery, sometimes JQuery or JQUERY"; var s2 = s1.replace(/jQuery/gi, "jQuery"); //s2 is now "jQuery, sometimes jQuery or jQuery"

The best way to find and replace substrings while ignoring case is to use a regular expression in the .replace() method. If you are not familiar with them, I’d suggest you at least look into them. They can use complex formulas for finding matches in strings. For now, I’ll just show you a simple expression.

As an example, to find all instances of the term jQuery when you may not know the case specified in the string, you would define the following regular expression. The g character specifies a global search (meaning not to ...

Get jQuery and JavaScript Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.