Solved

Javascript Regex Replace <img......> with <img....../>

Posted on 2004-04-06
4
2,084 Views
Last Modified: 2007-12-19
I'm new to regex, but I managed to get it working. My problem now is that if I run the replace function twice i get <img...../ />

my code:

var myregex = new RegExp("<img([^<>+]*)>", "g");
htmltext = htmltext.replace(myregex, "<img $1 />");

I tried adding the forward slash in :

var myregex = new RegExp("<img([^</>+]*)>", "g");
htmltext = htmltext.replace(myregex, "<img $1 />");

but that will jump out on src="blah/blah.jpg"

So I need to to replace <img.....> with <img...../>
But to leave <img......./> alone.
0
Comment
Question by:Mickah
  • 3
4 Comments
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 10764739
try this

<script>
var myregex = new RegExp("<img([^<>+]*)[^\/]>", "g");

htmltext='<img src=abc.gif >'
htmltext = htmltext.replace(myregex, "<img $1 />");
alert(htmltext)

htmltext='<img src=abc.gif />'
htmltext = htmltext.replace(myregex, "<img $1 />");
alert(htmltext)

htmltext='<img src=xyz/rst/abc.gif />'
htmltext = htmltext.replace(myregex, "<img $1 />");
alert(htm
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 10764748
bad paste

<script>
var myregex = new RegExp("<img([^<>+]*)[^\/]>", "g");

htmltext='<img src=abc.gif >'
htmltext = htmltext.replace(myregex, "<img $1 />");
alert(htmltext)

htmltext='<img src=abc.gif />'
htmltext = htmltext.replace(myregex, "<img $1 />");
alert(htmltext)

htmltext='<img src=xyz/rst/abc.gif />'
htmltext = htmltext.replace(myregex, "<img $1 />");
alert(htmltext)

</script>
0
 

Author Comment

by:Mickah
ID: 10764770
if i run that code several times it deletes beyond the <img..../> tag and screws with the rest of my htmltext
0
 
LVL 31

Accepted Solution

by:
GwynforWeb earned 125 total points
ID: 10764857
<script>
var myregex = new RegExp("<img([^<>+]*[^\/])>", "g");


htmltext='<img src=xyz/rst/abc.gif /><font face=arial>abcde fgh<font><img src=abc.gif><img src=abc.gif /><b>123 456</b>'

htmltext = htmltext.replace(myregex, "<img $1 />");
alert(htmltext)

htmltext = htmltext.replace(myregex, "<img $1 />");
alert(htmltext)

</script>
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

747 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

18 Experts available now in Live!

Get 1:1 Help Now