Forum Moderators: open

Message Too Old, No Replies

Simple string replace question

seems like a simple task for pros

         

leechieboi

10:25 am on Feb 14, 2004 (gmt 0)

10+ Year Member



I just started learing about string replace and the rest so i dont understand it fully but...

i need a simple command to replace one string (ie: <img src=image.jpg">) with another (ie: <iframe src="iframe.html">)

i dont even want to put up wut i've tried because its all just a mess

and the names of the files dont even have to be variables. i want to be able to replace specific strings.

any help would be appreciated

RonPK

11:48 am on Feb 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The easiest way to replace part of a string is by using regular expressions. Try something like this:

<script type="text/javascript">
var input = 'something <img src="image.jpg"> <b>whatever</b>';
var pattern = /<img src=\".*?">/g ;
var replaced = input.replace(pattern, '<iframe src="iframe.html">');
alert(replaced);
</script>

Welcome to WebmasterWorld, by the way!

leechieboi

2:23 pm on Feb 14, 2004 (gmt 0)

10+ Year Member



um i dont know if its because i dont understand your solution or you dont understand what i need but i need it so instead of a picture i get an iframe in its place

i'm not very sure what your code was supposed to do

birdbrain

3:55 pm on Feb 14, 2004 (gmt 0)



Hi there leechieboi,

Here is an example, using buttons
for the changeover, that you might
like to try...


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta content="mshtml 6.00.2800.1264" name="generator" />
<title>image to iframe and back</title>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
<style type="text/css">
/*<![CDATA[*/
#foo{position:absolute;top:20%;left:20%;display:none;}
#fool{position:absolute;top:20%;left:20%;display:block}
/*//]]>*/
</style>
</head>

<body>

<div>
<button onclick="document.getElementById('fool').style.display='none';
document.getElementById('foo').style.display='block'">iframe</button>
<button onclick="document.getElementById('fool').style.display='block';
document.getElementById('foo').style.display='none'">image</button>
</div>

<div><img id="fool" src="some.jpg"alt=""/></div>
<div><iframe id="foo"src="some.html"></iframe></div>

</body>
</html>

birdbrain

RonPK

8:54 am on Feb 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



leechieboi, please clarify how you intend to use the script. Is it supposed to work on a page that is already being displayed in the browser, or does it receive user input which should be modified?