Solved

Using VAriable in replace() using javascript

Posted on 2009-04-08
5
236 Views
Last Modified: 2012-06-22
Can I use variable in replace function for old and new string? I know that old new string can be variable.  How abt old string? Replace all "The" to "Ze" using variable not literals.

Thanks
<html>
<body>
 
<script type="text/javascript">
var str = "The Lord of the Rings: The Fellowship of the Ring"; 
 
var news = "ze";
 var old = "The"
var result = str.replace(/old/gi,news);
 
document.write(result);
</script>
 
</body>
</html>

Open in new window

0
Comment
Question by:ahmeix0l
[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
  • 4
5 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24094996
Here


<html>
<body>
 
<script type="text/javascript">
var str = "The Lord of the Rings: The Fellowship of the Ring"; 
 
var news = "ze";
var old = "The"
var reg = new RegExp(old,"gi")
var result = str.replace(reg,news);
 
document.write(result);
</script>
 
</body>
</html>

Open in new window

0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 24094999
Don't menzion ze war
0
 

Author Closing Comment

by:ahmeix0l
ID: 31567916
I think  str.replace(old,news); also does the job,
Thanks dude.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24104178
Not completely since you miss the gi.
Ps: my name is Michel :)
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24105132
See:
<html>
<body>
 
<script type="text/javascript">
var str = "The Lord of the Rings: The Fellowship of the Ring"; 
 
var news = "ze";
var old = "The"
var reg = new RegExp(old,"gi")
var result = str.replace(reg,news);
 
document.write(result);
var result = str.replace(old,news);
document.write('<hr>');
document.write(result);
 
</script>
 
</body>
</html>

Open in new window

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JavaScript Loan Calculator Error 3 35
Fix Form size HTML 16 31
Duplicate Elements with JS / jQuery 3 24
Aspx calendar pop up control 3 28
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'…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

726 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