O'Reilly logo

jQuery and JavaScript Phrasebook by Brad Dayley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required