Solved

Using VAriable in replace() using javascript

Posted on 2009-04-08
5
238 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

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. …
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…

717 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