0

The second 'senzajs' in the <map> is not being replaced but I would like it to...

Javascript

 <script  type="text/javascript">
  $("document").ready(function(){
     $('body').html($('body').html().replace('senzajs', 'js'));
   });
  </script>

HTML

  <div class="objectdiv">
   <a href="freegames/Classic/svgsenzajs/start.php/">
    <object class="svg" type="image/svg+xml"  data="svg/question0optimize1.svg" >
         <img src="png/question0optimize1.png" type="image/png" usemap="#mapping"/>    
     </object>   
   </a>
   <map name="mapping">
        <area shape="rect" coords="0,0,1000,1000" 
         href="freegames/Classic/pngsenzajs/start.php/" />
    </map>
 </div>

Why is this happening?

Moses
  • 9,033
  • 5
  • 44
  • 67
EnglishAdam
  • 1,380
  • 1
  • 19
  • 42

4 Answers4

4

Use .replace(/senzajs/g, 'js'). A regex with the g flag will match all occurrences instead of just the first one.

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
1

You need to use regex here otherwise replace only replace the first string found. /senzajs/g will match all found one, with exactly the same case. Add i to ignore the casing /senzajs/gi so even SENZJAS would be replaced.

<script  type="text/javascript">
  $("document").ready(function(){
     $('body').html($('body').html().replace(/senzajs/g, 'js'));
   });
  </script>
GillesC
  • 10,647
  • 3
  • 40
  • 55
1

The replace function as you've used it only replaces the first instance it finds. Use a regular expression to replace all instances:

var myNewString = myOldString.replace(/username/g, visitorName);

(I lifted this from http://www.tizag.com/javascriptT/javascript-string-replace.php.)

Phil Cairns
  • 758
  • 4
  • 15
0
 <script  type="text/javascript">
  $("document").ready(function(){
     $('body').html($('body').html().replace('/senzajs/gm', 'js'));
   });
  </script>
pylover
  • 7,670
  • 8
  • 51
  • 73