Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Using VAriable in replace() using javascript

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

609 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