Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

JavaScript: Case Insensitive Replace

How can I make this case insensitive?

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

Open in new window

Avatar of Rainer Jeschor
Rainer Jeschor
Flag of Germany image

Hi,
please try the last example from this page

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

HTH
Rainer
You could convert to uppercase first...

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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I was missing a closing quote and closing paren :

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

Open in new window

Avatar of hankknight

ASKER

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

Open in new window