?
Solved

Using VAriable in replace() using javascript

Posted on 2009-04-08
5
Medium Priority
?
244 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
  • 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…
Suggested Courses

807 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