Solved

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

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

831 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