Solved

JavaScript: Case Insensitive Replace

Posted on 2013-05-14
5
286 Views
Last Modified: 2013-05-15
How can I make this case insensitive?

var text = 'Hello World';
var r = 'World';
alert( text.replace(r,'EARTH') )

Open in new window

0
Comment
Question by:hankknight
5 Comments
 
LVL 44

Expert Comment

by:Rainer Jeschor
ID: 39166310
Hi,
please try the last example from this page

http://www.w3schools.com/jsref/jsref_replace.asp

HTH
Rainer
0
 
LVL 19

Expert Comment

by:Ken Butters
ID: 39166323
You could convert to uppercase first...

alert( text.toUpperCase().replace(r.toUpperCase(),'EARTH)

Open in new window

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39166349
0
 
LVL 19

Expert Comment

by:Ken Butters
ID: 39166435
I was missing a closing quote and closing paren :

Here is udpated code
alert( text.toUpperCase().replace(r.toUpperCase(),'EARTH'));

Open in new window

0
 
LVL 16

Author Comment

by:hankknight
ID: 39167571
//Thanks leakim971
var text = 'Hello World';
var r = 'World';
alert( text.replace(new RegExp(r,"i"),'EARTH') )

Open in new window

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now