?
Solved

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

Posted on 2004-04-06
4
Medium Priority
?
2,109 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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…
Suggested Courses

829 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