Solved

Using VAriable in replace() using javascript

Posted on 2009-04-08
5
225 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
Comment Utility
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
Comment Utility
Don't menzion ze war
0
 

Author Closing Comment

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

Expert Comment

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

Expert Comment

by:Michel Plungjan
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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. …
When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now