Solved

JavaScript: Case Insensitive Replace

Posted on 2013-05-14
5
310 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

691 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