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
Solved

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

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
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…

765 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