Solved

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

Posted on 2004-04-06
4
2,096 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
[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
  • 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

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-…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

696 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