jquery - Replace text with img based off of text -


i want replace text img using part of itself:

<li class='custom'>  <legend>images:</legend>  {[img.png][1desc][2desc]} {[img2.png][1desc2][2desc2]} </li> 

i want appear this:

<li class='custom'>  <legend>images:</legend>  <img src="img.png" title="1desc - 2desc"/> <img src="img2.png" title="1desc2 - 2desc2"/> </li> 

current code using(doesn't work):

<script> function texttoimg(theimg) {   return theimg.replace(            /\{\s*[\s*(.*?)\s*]\s*[\s*(.*?)\s*]\s*[\s*(.*?)\s*]\s*\}/gi,            '<img src="$1" title="$2 - $3"/>'          ); }  jquery('li.custom').each(function() {      current = jquery(this);      img = texttoimg(current.html());      current.html(img);  }); </script> 

it looks [ , ] in regular expression not being escaped. try (demo):

function texttoimg(theimg) {   return theimg.replace(            /\{\s*\[\s*(.*?)\s*\]\s*\[\s*(.*?)\s*\]\s*\[\s*(.*?)\s*\]\s*\}/gi,            '<img src="$1" title="$2 - $3"/>'          ); } 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -